File: 50_client-c_bufferoverflow_fix.dpatch

package info (click to toggle)
cgiirc 0.5.4-6sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 916 kB
  • ctags: 424
  • sloc: perl: 8,904; sh: 821; ansic: 132; makefile: 54
file content (162 lines) | stat: -rw-r--r-- 4,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /bin/sh /usr/share/dpatch/dpatch-run
## 50_client-c_bufferoverflow_fix.dpatch by Elrond <elrond+debian.org@samba-tng.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Fix various buffer overflows in client.c.


--- cgiirc-0.5.4/client.c.orig	2002-05-11 16:52:18.000000000 +0200
+++ cgiirc-0.5.4/client.c	2006-05-02 21:04:19.000000000 +0200
@@ -1,5 +1,5 @@
 /* CGI:IRC C Helper CGI
- * Copyright (c) David Leadbeater 2002
+ * Copyright (c) David Leadbeater 2002-2006
  * Released Under the GNU GPLv2 or Later
  * NO WARRANTY - See GNU GPL for more
  * $Id: client.c,v 1.9 2002/05/11 14:52:18 dgl Exp $
@@ -20,9 +20,9 @@
 
 int unix_connect(char *where);
 int error(char *error);
-int readinput(char *params);
-int get_rand(char *params, char *rand);
-int get_cookie(char *cookie);
+int readinput(char *params, size_t len);
+int get_rand(char *params, char *rand, size_t len);
+int get_cookie(char *cookie, size_t len);
 
 int main(void) {
    int fd;
@@ -31,20 +31,23 @@
    char tmp[2148]; /* I decided to stop adding comments after here */
    char cookie[100];
    
-   if(!readinput(params)) error("No input found\n");
-   if(!get_rand(params, rand)) error("Random Value not found\n");
+   if(!readinput(params, sizeof params)) error("No input found\n");
+   if(!get_rand(params, rand, sizeof rand)) error("Random Value not found\n");
 
-   if(get_cookie(cookie)) {
+   if(get_cookie(cookie, sizeof(cookie))) {
       char tmp2[2148]; /* I'm sure there's a better way of doing this.. */
-      strncpy(tmp2, params, 2147);
-      snprintf(params, 2148, "COOKIE=%s&%s", cookie, tmp2);
+      strncpy(tmp2, params, sizeof tmp2);
+      tmp2[sizeof(tmp2) - 1] = '\0';
+      snprintf(params, sizeof params, "COOKIE=%s&%s", cookie, tmp2);
+      params[sizeof(params) - 1] = '\0';
    }
 
    fd = unix_connect(rand);
    send(fd, params, strlen(params), 0);
    send(fd, "\n", 1, 0);
 
-   while(read(fd, tmp, 2048) > 0) {
+   while(read(fd, tmp, sizeof(tmp) - 1) > 0) {
+	  tmp[sizeof(tmp) - 1] = '\0';
 	  printf("%s",tmp);
    }
 
@@ -57,7 +60,7 @@
    exit(1);
 }
 
-int readinput(char *params) {
+int readinput(char *params, size_t len) {
    char request[10];
 
    if(!getenv("REQUEST_METHOD")) return 0;
@@ -66,8 +69,8 @@
    if(!strlen(request)) return 0;
 
    if(strncmp(request, "GET", 3) == 0) {
-	  strncpy(params, getenv("QUERY_STRING"), 2048);
-	  params[2048] = 0;
+	  strncpy(params, getenv("QUERY_STRING"), len);
+	  params[len - 1] = 0;
 	  if(!strlen(params)) return 0;
       return 1;
    }else if(strncmp(request, "POST", 4) == 0) {
@@ -75,7 +78,8 @@
 	  if(!getenv("CONTENT_LENGTH")) return 0;
 	  length = atoi(getenv("CONTENT_LENGTH"));
 	  if(!length || length == 0) return 0;
-	  fread(params, length > 2048 ? 2048 : length, 1, stdin);
+	  length = (length >= len ? len - 1 : length);
+	  fread(params, length, 1, stdin);
 	  params[length] = 0;
 	  return 1;
    }else{
@@ -83,7 +87,7 @@
    }
 }
 
-int get_rand(char *params, char *rand) { 
+int get_rand(char *params, char *rand, size_t len) { 
    char *ptr, *end_ptr;
    int r = 0, i = 0;
    ptr = params;
@@ -92,7 +96,7 @@
    for(;ptr < end_ptr; ptr++) {
 	  if(r == 1) {
 		 if(*ptr == '&') break;
-		 if(i > 48) break;
+		 if(i > len - 2) break;
 		 if(isalpha(*ptr) || isdigit(*ptr)) {
 		    rand[i] = *ptr;
 		    i++;
@@ -107,26 +111,22 @@
    return 0;
 }
 
-int get_cookie(char *cookie) {
+int get_cookie(char *cookie, size_t len) {
    char ctmp[1024];
-   char *sptr, *end_ptr;
-   int i;
+   char *sptr;
+   size_t i;
 
    if(!getenv("HTTP_COOKIE")) return 0;
-   strncpy(ctmp, getenv("HTTP_COOKIE"), 1023);
+   strncpy(ctmp, getenv("HTTP_COOKIE"), sizeof ctmp - 1);
+   ctmp[sizeof(ctmp) - 1] = '\0';
 
    sptr = strstr(ctmp, "cgiircauth=");
    if(sptr == NULL) return 0;
    if(strlen(sptr) < 12) return 0;
    sptr += 11;
-   end_ptr = sptr + (strlen(sptr) < 99 ? strlen(sptr) : 99);
 
-   i = 0;
-   while((int)sptr < (int)end_ptr && *sptr != ';') {
+   for (i = 0; *sptr && *sptr != ';' && i < (len-1); i++, sptr++)
       cookie[i] = *sptr;
-      sptr++;
-      i++;
-   }
    cookie[i] = '\0';
    return 1;
 }
@@ -138,15 +138,17 @@
    char filename[100], errmsg[100];
 
    len = strlen(TMPLOCATION) + strlen(where) + 6;
-   if(len > 100) error("Too long");
-   snprintf(filename, len, "%s%s/sock", TMPLOCATION, where);
-   filename[len] = 0;
+   if(len > sizeof(filename))
+     error("Too long");
+   snprintf(filename, sizeof(filename), "%s%s/sock", TMPLOCATION, where);
+   filename[len-1] = 0;
 
    sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if(sock == -1) error("socket() error\n");
 
    saddr.sun_family = AF_UNIX;
-   strcpy(saddr.sun_path, filename);
+   strncpy(saddr.sun_path, filename, sizeof(saddr.sun_path));
+   saddr.sun_path[sizeof(saddr.sun_path) - 1] = '\0';
 
    if(connect(sock, (struct sockaddr *)&saddr, SUN_LEN(&saddr)) == -1) {
           switch(errno) {