diff options
author | Robert Griebl <griebl@gmx.de> | 2002-06-04 19:33:58 +0000 |
---|---|---|
committer | Robert Griebl <griebl@gmx.de> | 2002-06-04 19:33:58 +0000 |
commit | bc28f7a1e19397666e687a6a1ba38deff9fd1030 (patch) | |
tree | a32e4546800a0046655bd8cfb0196953515255dc | |
parent | c06391be0d468eba7944f150636ba879487d43ca (diff) | |
download | busybox-w32-bc28f7a1e19397666e687a6a1ba38deff9fd1030.tar.gz busybox-w32-bc28f7a1e19397666e687a6a1ba38deff9fd1030.tar.bz2 busybox-w32-bc28f7a1e19397666e687a6a1ba38deff9fd1030.zip |
Cleaned up Erik's fgets -> read patch and fixed a buffer overflow
-rw-r--r-- | modutils/modprobe.c | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/modutils/modprobe.c b/modutils/modprobe.c index a72028fa2..fbe18b640 100644 --- a/modutils/modprobe.c +++ b/modutils/modprobe.c | |||
@@ -44,9 +44,38 @@ static struct dep_t *depend; | |||
44 | static int autoclean, show_only, quiet, do_syslog, verbose; | 44 | static int autoclean, show_only, quiet, do_syslog, verbose; |
45 | 45 | ||
46 | 46 | ||
47 | /* Jump through hoops to simulate how fgets() grabs just one line at a | ||
48 | * time... Don't use any stdio since modprobe gets called from a kernel | ||
49 | * thread and stdio junk can overflow the limited stack... | ||
50 | */ | ||
51 | static char *reads ( int fd, char *buffer, size_t len ) | ||
52 | { | ||
53 | int n = read ( fd, buffer, len ); | ||
54 | |||
55 | if ( n > 0 ) { | ||
56 | char *p; | ||
57 | |||
58 | buffer [len-1] = 0; | ||
59 | p = strchr ( buffer, '\n' ); | ||
60 | |||
61 | if ( p ) { | ||
62 | off_t offset; | ||
63 | |||
64 | offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset | ||
65 | lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n | ||
66 | |||
67 | p[1] = 0; | ||
68 | } | ||
69 | return buffer; | ||
70 | } | ||
71 | |||
72 | else | ||
73 | return 0; | ||
74 | } | ||
75 | |||
47 | static struct dep_t *build_dep ( void ) | 76 | static struct dep_t *build_dep ( void ) |
48 | { | 77 | { |
49 | int fd, n; | 78 | int fd; |
50 | struct utsname un; | 79 | struct utsname un; |
51 | struct dep_t *first = 0; | 80 | struct dep_t *first = 0; |
52 | struct dep_t *current = 0; | 81 | struct dep_t *current = 0; |
@@ -66,27 +95,13 @@ static struct dep_t *build_dep ( void ) | |||
66 | strcat ( filename, un.release ); | 95 | strcat ( filename, un.release ); |
67 | strcat ( filename, "/modules.dep" ); | 96 | strcat ( filename, "/modules.dep" ); |
68 | 97 | ||
69 | if ((fd = open ( filename, O_RDONLY )) < 0) | 98 | if (( fd = open ( filename, O_RDONLY )) < 0 ) |
70 | return 0; | 99 | return 0; |
71 | 100 | ||
72 | while ( (n = read(fd, buffer, 255)) > 0) { | 101 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
73 | int l; | 102 | int l = xstrlen ( buffer ); |
74 | char *p = 0; | 103 | char *p = 0; |
75 | 104 | ||
76 | /* Jump through hoops to simulate how fgets() grabs just one line at a | ||
77 | * time... Don't use any stdio since modprobe gets called from a kernel | ||
78 | * thread and stdio junk can overflow the limited stack... */ | ||
79 | p = strchr ( buffer, '\n' ); | ||
80 | if (p) { | ||
81 | off_t offset; | ||
82 | /* Get the current file descriptor offset */ | ||
83 | offset = lseek(fd, 0L, SEEK_CUR); | ||
84 | /* Set the file descriptor offset to right after the \n */ | ||
85 | lseek(fd, offset-n+p-buffer+1, SEEK_SET); | ||
86 | *(p+1)='\0'; | ||
87 | } | ||
88 | |||
89 | l = xstrlen ( buffer ); | ||
90 | while ( isspace ( buffer [l-1] )) { | 105 | while ( isspace ( buffer [l-1] )) { |
91 | buffer [l-1] = 0; | 106 | buffer [l-1] = 0; |
92 | l--; | 107 | l--; |
@@ -182,29 +197,16 @@ static struct dep_t *build_dep ( void ) | |||
182 | 197 | ||
183 | // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) ! | 198 | // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) ! |
184 | 199 | ||
185 | if ((fd = open ( "/etc/modules.conf", O_RDONLY )) < 0) | 200 | if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 ) |
186 | if ((fd = open ( "/etc/conf.modules", O_RDONLY )) < 0) | 201 | if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 ) |
187 | return first; | 202 | return first; |
188 | 203 | ||
189 | continuation_line = 0; | 204 | continuation_line = 0; |
190 | while ( read(fd, buffer, 255) > 0) { | 205 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
191 | int l; | 206 | int l; |
192 | char *p; | 207 | char *p; |
193 | 208 | ||
194 | /* Jump through hoops to simulate how fgets() grabs just one line at a | 209 | p = strchr ( buffer, '#' ); |
195 | * time... Don't use any stdio since modprobe gets called from a kernel | ||
196 | * thread and stdio junk can overflow the limited stack... */ | ||
197 | p = strchr ( buffer, '\n' ); | ||
198 | if (p) { | ||
199 | off_t offset; | ||
200 | /* Get the current file descriptor offset */ | ||
201 | offset = lseek(fd, 0L, SEEK_CUR); | ||
202 | /* Set the file descriptor offset to right after the \n */ | ||
203 | lseek(fd, offset-n+p-buffer+1, SEEK_SET); | ||
204 | *(p+1)='\0'; | ||
205 | } | ||
206 | |||
207 | p = strchr ( buffer, '#' ); | ||
208 | if ( p ) | 210 | if ( p ) |
209 | *p = 0; | 211 | *p = 0; |
210 | 212 | ||