Hi,

The appended patch should fix this issue.
It's based on the upstream patches in 0.5.8.

This is the result of Mario already reviewing it.
Everybody else is invited to review it too.


To create a new package:

- Place the .dpatch in debian/patches
- Add 50_client-c_bufferoverflow_fix.dpatch to
  debian/patches/
- Create a new changelog entry.


To Do / Done:

* I verified, that the above builds a fresh package
* Testing is needed
* Tag this bug "patch"
* Let the security team to their job


    Elrond
#! /bin/sh /usr/share/dpatch/dpatch-run
## 50_client-c_bufferoverflow_fix.dpatch by Elrond <[EMAIL PROTECTED]>
##
## 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) {

Reply via email to