Project

General

Profile

0005-zero-sockaddr-structs-before-use_patch.txt

mackyle, 2015-12-03 22:46

 
1
From 37649f237ef5e89223b06c43f77fdce728f4de0e Mon Sep 17 00:00:00 2001
2
From: "Kyle J. McKay" <mackyle@gmail.com>
3
Date: Thu, 3 Dec 2015 11:20:33 -0800
4
Subject: [PATCH] zero sockaddr structs before use
5

    
6
When a sockaddr_un, sockaddr_in or sockaddr_in6 structure
7
is allocated on the stack or heap, it may contain random
8
byte values.
9

    
10
The "unused" and "reserved" parts must be zerod otherwise
11
unexpected failures may occur.  The simplest way to do
12
this and be compatible with various platforms' struct
13
layouts is just to memset them to 0.
14

    
15
Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
16
---
17
 src/mod_fastcgi.c | 8 ++++----
18
 src/mod_scgi.c    | 8 ++++----
19
 2 files changed, 8 insertions(+), 8 deletions(-)
20

    
21
diff --git a/src/mod_fastcgi.c b/src/mod_fastcgi.c
22
index 63405ee0..0c3620e1 100644
23
--- a/src/mod_fastcgi.c
24
+++ b/src/mod_fastcgi.c
25
@@ -859,9 +859,8 @@ static int fcgi_spawn_connection(server *srv,
26
 	}
27
 
28
 	if (!buffer_string_is_empty(proc->unixsocket)) {
29
-		memset(&fcgi_addr, 0, sizeof(fcgi_addr));
30
-
31
 #ifdef HAVE_SYS_UN_H
32
+		memset(&fcgi_addr_un, 0, sizeof(fcgi_addr_un));
33
 		fcgi_addr_un.sun_family = AF_UNIX;
34
 		if (buffer_string_length(proc->unixsocket) + 1 > sizeof(fcgi_addr_un.sun_path)) {
35
 			log_error_write(srv, __FILE__, __LINE__, "sB",
36
@@ -889,6 +888,7 @@ static int fcgi_spawn_connection(server *srv,
37
 		return -1;
38
 #endif
39
 	} else {
40
+		memset(&fcgi_addr_in, 0, sizeof(fcgi_addr_in));
41
 		fcgi_addr_in.sin_family = AF_INET;
42
 
43
 		if (buffer_string_is_empty(host->host)) {
44
@@ -1660,11 +1660,10 @@ static connection_result_t fcgi_establish_connection(server *srv, handler_ctx *h
45
 	fcgi_proc *proc   = hctx->proc;
46
 	int fcgi_fd       = hctx->fd;
47
 
48
-	memset(&fcgi_addr, 0, sizeof(fcgi_addr));
49
-
50
 	if (!buffer_string_is_empty(proc->unixsocket)) {
51
 #ifdef HAVE_SYS_UN_H
52
 		/* use the unix domain socket */
53
+		memset(&fcgi_addr_un, 0, sizeof(fcgi_addr_un));
54
 		fcgi_addr_un.sun_family = AF_UNIX;
55
 		if (buffer_string_length(proc->unixsocket) + 1 > sizeof(fcgi_addr_un.sun_path)) {
56
 			log_error_write(srv, __FILE__, __LINE__, "sB",
57
@@ -1691,6 +1690,7 @@ static connection_result_t fcgi_establish_connection(server *srv, handler_ctx *h
58
 		return CONNECTION_DEAD;
59
 #endif
60
 	} else {
61
+		memset(&fcgi_addr_in, 0, sizeof(fcgi_addr_in));
62
 		fcgi_addr_in.sin_family = AF_INET;
63
 		if (!buffer_string_is_empty(host->host)) {
64
 			if (0 == inet_aton(host->host->ptr, &(fcgi_addr_in.sin_addr))) {
65
diff --git a/src/mod_scgi.c b/src/mod_scgi.c
66
index 4c629a76..bd2dbb67 100644
67
--- a/src/mod_scgi.c
68
+++ b/src/mod_scgi.c
69
@@ -666,9 +666,8 @@ static int scgi_spawn_connection(server *srv,
70
 	}
71
 
72
 	if (!buffer_string_is_empty(proc->socket)) {
73
-		memset(&scgi_addr, 0, sizeof(scgi_addr));
74
-
75
 #ifdef HAVE_SYS_UN_H
76
+		memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
77
 		scgi_addr_un.sun_family = AF_UNIX;
78
 		if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
79
 			log_error_write(srv, __FILE__, __LINE__, "sB",
80
@@ -692,6 +691,7 @@ static int scgi_spawn_connection(server *srv,
81
 		return -1;
82
 #endif
83
 	} else {
84
+		memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
85
 		scgi_addr_in.sin_family = AF_INET;
86
 
87
 		if (buffer_string_is_empty(host->host)) {
88
@@ -1339,11 +1339,10 @@ static int scgi_establish_connection(server *srv, handler_ctx *hctx) {
89
 	scgi_proc *proc   = hctx->proc;
90
 	int scgi_fd       = hctx->fd;
91
 
92
-	memset(&scgi_addr, 0, sizeof(scgi_addr));
93
-
94
 	if (!buffer_string_is_empty(proc->socket)) {
95
 #ifdef HAVE_SYS_UN_H
96
 		/* use the unix domain socket */
97
+		memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
98
 		scgi_addr_un.sun_family = AF_UNIX;
99
 		if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
100
 			log_error_write(srv, __FILE__, __LINE__, "sB",
101
@@ -1364,6 +1363,7 @@ static int scgi_establish_connection(server *srv, handler_ctx *hctx) {
102
 		return -1;
103
 #endif
104
 	} else {
105
+		memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
106
 		scgi_addr_in.sin_family = AF_INET;
107
 		if (0 == inet_aton(host->host->ptr, &(scgi_addr_in.sin_addr))) {
108
 			log_error_write(srv, __FILE__, __LINE__, "sbs",
109
-- 
110
2.4.10
111