]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
xtables: improve get_modprobe handling
authorPhil Oester <kernel@linuxace.com>
Mon, 27 May 2013 06:55:11 +0000 (06:55 +0000)
committerPablo Neira Ayuso <pablo@soleta.eu>
Wed, 29 May 2013 17:45:46 +0000 (19:45 +0200)
In bug #455, Dmitry V. Levin proposed a more robust get_modprobe
implementation.  The patch below is a version of his patch,
updated to apply to current git.

This closes bug #455.

Signed-off-by: Phil Oester <kernel@linuxace.com>
Signed-off-by: Pablo Neira Ayuso <pablo@soleta.eu>
libxtables/xtables.c

index 009ab9115f6fd687a762a2552f89ac0b81ee1a42..ebc77b6c3143a9c246dd85d3a4ce2fcf874b7e31 100644 (file)
@@ -305,8 +305,8 @@ static char *get_modprobe(void)
 {
        int procfile;
        char *ret;
+       int count;
 
-#define PROCFILE_BUFSIZ        1024
        procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
        if (procfile < 0)
                return NULL;
@@ -316,19 +316,19 @@ static char *get_modprobe(void)
                exit(1);
        }
 
-       ret = malloc(PROCFILE_BUFSIZ);
+       ret = malloc(PATH_MAX);
        if (ret) {
-               memset(ret, 0, PROCFILE_BUFSIZ);
-               switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
-               case -1: goto fail;
-               case PROCFILE_BUFSIZ: goto fail; /* Partial read.  Wierd */
+               count = read(procfile, ret, PATH_MAX);
+               if (count > 0 && count < PATH_MAX)
+               {
+                       if (ret[count - 1] == '\n')
+                               ret[count - 1] = '\0';
+                       else
+                               ret[count] = '\0';
+                       close(procfile);
+                       return ret;
                }
-               if (ret[strlen(ret)-1]=='\n') 
-                       ret[strlen(ret)-1]=0;
-               close(procfile);
-               return ret;
        }
- fail:
        free(ret);
        close(procfile);
        return NULL;