aboutsummaryrefslogtreecommitdiff
path: root/libbb/appletlib.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2011-05-16 12:21:31 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2011-05-16 12:21:31 +0200
commit9be4702a304863095d20181b5632ee7e0a4acdc4 (patch)
treea762d7482c1207417d0dee719ef4887b8f100b06 /libbb/appletlib.c
parent9a296fbeab604c9b64189b02a79654ec0aa68130 (diff)
downloadbusybox-w32-9be4702a304863095d20181b5632ee7e0a4acdc4.tar.gz
busybox-w32-9be4702a304863095d20181b5632ee7e0a4acdc4.tar.bz2
busybox-w32-9be4702a304863095d20181b5632ee7e0a4acdc4.zip
main: free suid_config list after use
function old new delta run_applet_no_and_exit 438 450 +12 ifupdown_main 2147 2149 +2 writeFileToTarball 1325 1326 +1 pidof_main 244 245 +1 last_main 896 897 +1 grep_main 779 780 +1 find_list_entry2 121 122 +1 tar_main 835 833 -2 llist_unlink 28 26 -2 llist_rev 23 21 -2 main 791 782 -9 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 7/4 up/down: 19/-15) Total: 4 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/appletlib.c')
-rw-r--r--libbb/appletlib.c63
1 files changed, 30 insertions, 33 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 03f712821..269cc5bbf 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -27,12 +27,13 @@
27 * FEATURE_INSTALLER or FEATURE_SUID will still link printf routines in. :( 27 * FEATURE_INSTALLER or FEATURE_SUID will still link printf routines in. :(
28 */ 28 */
29#include "busybox.h" 29#include "busybox.h"
30#include <assert.h> 30
31#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ 31#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
32 || defined(__APPLE__) \ 32 || defined(__APPLE__) \
33 ) 33 )
34# include <malloc.h> /* for mallopt */ 34# include <malloc.h> /* for mallopt */
35#endif 35#endif
36
36/* Try to pull in PAGE_SIZE */ 37/* Try to pull in PAGE_SIZE */
37#ifdef __linux__ 38#ifdef __linux__
38# include <sys/user.h> 39# include <sys/user.h>
@@ -40,6 +41,9 @@
40#ifdef __GNU__ /* Hurd */ 41#ifdef __GNU__ /* Hurd */
41# include <mach/vm_param.h> 42# include <mach/vm_param.h>
42#endif 43#endif
44#ifndef PAGE_SIZE
45# define PAGE_SIZE (4*1024) /* guess */
46#endif
43 47
44 48
45/* Declare <applet>_main() */ 49/* Declare <applet>_main() */
@@ -47,7 +51,6 @@
47#include "applets.h" 51#include "applets.h"
48#undef PROTOTYPES 52#undef PROTOTYPES
49 53
50
51/* Include generated applet names, pointers to <applet>_main, etc */ 54/* Include generated applet names, pointers to <applet>_main, etc */
52#include "applet_tables.h" 55#include "applet_tables.h"
53/* ...and if applet_tables generator says we have only one applet... */ 56/* ...and if applet_tables generator says we have only one applet... */
@@ -58,9 +61,9 @@
58# define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__ 61# define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__
59#endif 62#endif
60 63
61
62#include "usage_compressed.h" 64#include "usage_compressed.h"
63 65
66
64#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE 67#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
65static const char usage_messages[] ALIGN1 = UNPACKED_USAGE; 68static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
66#else 69#else
@@ -233,12 +236,12 @@ IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
233 236
234# if ENABLE_FEATURE_SUID_CONFIG 237# if ENABLE_FEATURE_SUID_CONFIG
235 238
236/* applets[] is const, so we have to define this "override" structure */ 239static struct suid_config_t {
237static struct BB_suid_config { 240 /* next ptr must be first: this struct needs to be llist-compatible */
238 int m_applet; 241 struct suid_config_t *m_next;
239 struct bb_uidgid_t m_ugid; 242 struct bb_uidgid_t m_ugid;
243 int m_applet;
240 mode_t m_mode; 244 mode_t m_mode;
241 struct BB_suid_config *m_next;
242} *suid_config; 245} *suid_config;
243 246
244static bool suid_cfg_readable; 247static bool suid_cfg_readable;
@@ -247,13 +250,10 @@ static bool suid_cfg_readable;
247static int ingroup(uid_t u, gid_t g) 250static int ingroup(uid_t u, gid_t g)
248{ 251{
249 struct group *grp = getgrgid(g); 252 struct group *grp = getgrgid(g);
250
251 if (grp) { 253 if (grp) {
252 char **mem; 254 char **mem;
253
254 for (mem = grp->gr_mem; *mem; mem++) { 255 for (mem = grp->gr_mem; *mem; mem++) {
255 struct passwd *pwd = getpwnam(*mem); 256 struct passwd *pwd = getpwnam(*mem);
256
257 if (pwd && (pwd->pw_uid == u)) 257 if (pwd && (pwd->pw_uid == u))
258 return 1; 258 return 1;
259 } 259 }
@@ -296,17 +296,12 @@ static const unsigned short mode_mask[] ALIGN2 = {
296 296
297static void parse_config_file(void) 297static void parse_config_file(void)
298{ 298{
299 struct BB_suid_config *sct_head; 299 struct suid_config_t *sct_head;
300 struct BB_suid_config *sct;
301 int applet_no; 300 int applet_no;
302 FILE *f; 301 FILE *f;
303 const char *errmsg; 302 const char *errmsg;
304 char *s;
305 char *e;
306 int i;
307 unsigned lc; 303 unsigned lc;
308 smallint section; 304 smallint section;
309 char buffer[256];
310 struct stat st; 305 struct stat st;
311 306
312 ruid = getuid(); 307 ruid = getuid();
@@ -327,9 +322,10 @@ static void parse_config_file(void)
327 section = lc = 0; 322 section = lc = 0;
328 323
329 while (1) { 324 while (1) {
330 s = buffer; 325 char buffer[256];
326 char *s;
331 327
332 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */ 328 if (!fgets(buffer, sizeof(buffer), f)) { /* Are we done? */
333 // Looks like bloat 329 // Looks like bloat
334 //if (ferror(f)) { /* Make sure it wasn't a read error. */ 330 //if (ferror(f)) { /* Make sure it wasn't a read error. */
335 // errmsg = "reading"; 331 // errmsg = "reading";
@@ -340,6 +336,7 @@ static void parse_config_file(void)
340 return; 336 return;
341 } 337 }
342 338
339 s = buffer;
343 lc++; /* Got a (partial) line. */ 340 lc++; /* Got a (partial) line. */
344 341
345 /* If a line is too long for our buffer, we consider it an error. 342 /* If a line is too long for our buffer, we consider it an error.
@@ -368,7 +365,7 @@ static void parse_config_file(void)
368 /* Unlike the old code, we ignore leading and trailing 365 /* Unlike the old code, we ignore leading and trailing
369 * whitespace for the section name. We also require that 366 * whitespace for the section name. We also require that
370 * there are no stray characters after the closing bracket. */ 367 * there are no stray characters after the closing bracket. */
371 e = strchr(s, ']'); 368 char *e = strchr(s, ']');
372 if (!e /* Missing right bracket? */ 369 if (!e /* Missing right bracket? */
373 || e[1] /* Trailing characters? */ 370 || e[1] /* Trailing characters? */
374 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */ 371 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
@@ -399,7 +396,7 @@ static void parse_config_file(void)
399 * where both key and value could contain inner whitespace. */ 396 * where both key and value could contain inner whitespace. */
400 397
401 /* First get the key (an applet name in our case). */ 398 /* First get the key (an applet name in our case). */
402 e = strchr(s, '='); 399 char *e = strchr(s, '=');
403 if (e) { 400 if (e) {
404 s = get_trimmed_slice(s, e); 401 s = get_trimmed_slice(s, e);
405 } 402 }
@@ -414,6 +411,9 @@ static void parse_config_file(void)
414 * up when the busybox configuration is changed. */ 411 * up when the busybox configuration is changed. */
415 applet_no = find_applet_by_name(s); 412 applet_no = find_applet_by_name(s);
416 if (applet_no >= 0) { 413 if (applet_no >= 0) {
414 int i;
415 struct suid_config_t *sct;
416
417 /* Note: We currently don't check for duplicates! 417 /* Note: We currently don't check for duplicates!
418 * The last config line for each applet will be the 418 * The last config line for each applet will be the
419 * one used since we insert at the head of the list. 419 * one used since we insert at the head of the list.
@@ -453,7 +453,7 @@ static void parse_config_file(void)
453 goto pe_label; 453 goto pe_label;
454 } 454 }
455 455
456 *e++ = ':'; /* get_uidgid doesn't understand user.group */ 456 *e = ':'; /* get_uidgid doesn't understand user.group */
457 if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) { 457 if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) {
458 errmsg = "unknown user/group"; 458 errmsg = "unknown user/group";
459 goto pe_label; 459 goto pe_label;
@@ -477,15 +477,11 @@ static void parse_config_file(void)
477 } /* while (1) */ 477 } /* while (1) */
478 478
479 pe_label: 479 pe_label:
480 fclose(f);
480 bb_error_msg("parse error in %s, line %u: %s", config_file, lc, errmsg); 481 bb_error_msg("parse error in %s, line %u: %s", config_file, lc, errmsg);
481 482
482 fclose(f);
483 /* Release any allocated memory before returning. */ 483 /* Release any allocated memory before returning. */
484 while (sct_head) { 484 llist_free((llist_t*)sct_head, NULL);
485 sct = sct_head->m_next;
486 free(sct_head);
487 sct_head = sct;
488 }
489} 485}
490# else 486# else
491static inline void parse_config_file(void) 487static inline void parse_config_file(void)
@@ -507,7 +503,7 @@ static void check_suid(int applet_no)
507# if ENABLE_FEATURE_SUID_CONFIG 503# if ENABLE_FEATURE_SUID_CONFIG
508 if (suid_cfg_readable) { 504 if (suid_cfg_readable) {
509 uid_t uid; 505 uid_t uid;
510 struct BB_suid_config *sct; 506 struct suid_config_t *sct;
511 mode_t m; 507 mode_t m;
512 508
513 for (sct = suid_config; sct; sct = sct->m_next) { 509 for (sct = suid_config; sct; sct = sct->m_next) {
@@ -545,7 +541,7 @@ static void check_suid(int applet_no)
545 541
546 if (setresuid(-1, uid, uid)) 542 if (setresuid(-1, uid, uid))
547 bb_perror_msg_and_die("setresuid"); 543 bb_perror_msg_and_die("setresuid");
548 return; 544 goto ret;
549 } 545 }
550# if !ENABLE_FEATURE_SUID_CONFIG_QUIET 546# if !ENABLE_FEATURE_SUID_CONFIG_QUIET
551 { 547 {
@@ -553,7 +549,7 @@ static void check_suid(int applet_no)
553 549
554 if (!onetime) { 550 if (!onetime) {
555 onetime = 1; 551 onetime = 1;
556 fprintf(stderr, "Using fallback suid method\n"); 552 bb_error_msg("using fallback suid method");
557 } 553 }
558 } 554 }
559# endif 555# endif
@@ -568,6 +564,10 @@ static void check_suid(int applet_no)
568 xsetgid(rgid); /* drop all privileges */ 564 xsetgid(rgid); /* drop all privileges */
569 xsetuid(ruid); 565 xsetuid(ruid);
570 } 566 }
567 ret: ;
568# if ENABLE_FEATURE_SUID_CONFIG
569 llist_free((llist_t*)suid_config, NULL);
570# endif
571} 571}
572# else 572# else
573# define check_suid(x) ((void)0) 573# define check_suid(x) ((void)0)
@@ -784,9 +784,6 @@ int main(int argc UNUSED_PARAM, char **argv)
784#endif 784#endif
785{ 785{
786 /* Tweak malloc for reduced memory consumption */ 786 /* Tweak malloc for reduced memory consumption */
787#ifndef PAGE_SIZE
788# define PAGE_SIZE (4*1024) /* guess */
789#endif
790#ifdef M_TRIM_THRESHOLD 787#ifdef M_TRIM_THRESHOLD
791 /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory 788 /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
792 * to keep before releasing to the OS 789 * to keep before releasing to the OS