From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10952f,b69d377feb5a8a2 X-Google-Attributes: gid10952f,public X-Google-Thread: fcda7,d4c1f7c0fe672a4 X-Google-Attributes: gidfcda7,public X-Google-Thread: 1070f0,d4c1f7c0fe672a4 X-Google-Attributes: gid1070f0,public X-Google-Thread: 1020c6,d4c1f7c0fe672a4 X-Google-Attributes: gid1020c6,public X-Google-Thread: 10872d,d4c1f7c0fe672a4 X-Google-Attributes: gid10872d,public X-Google-Thread: 1164f8,d4c1f7c0fe672a4 X-Google-Attributes: gid1164f8,public From: dsiebert@icaen.uiowa.edu (Doug Siebert) Subject: Re: Secure cleaning of /tmp was Different Unixes pros & cons Date: 1998/09/22 Message-ID: <6u77e4$o8l$1@server05.icaen.uiowa.edu> X-Deja-AN: 393529953 References: <35F71814.1E0B911C@iakom.com> <360674DF.C1EDCEAF@llnl.gov> <6u5vcg$92r@web.nmti.com> <360698A2.1F42719C@llnl.gov> Organization: Iowa Computer Aided Engineering Network, University of Iowa Reply-To: douglas-siebert@uiowa.edu Newsgroups: comp.unix.admin,comp.sys.hp.hpux,comp.sys.sgi.admin,comp.sys.sun.admin,comp.sys.dec,comp.security.unix Barry Margolin writes: >It's not really a security problem, but it is a correctness problem. If, >during the window, the old file gets deleted by some other process and >someone creates a new file with the same name, their new file will be >deleted even though it's not yet 7 days old. This is a pretty tiny chance -- if you check the file at the last second before the unlink, the odds are so small it isn't worth worrying about. This sort of accidental deletion isn't the same thing as a security race condition where an attacker can make a system arbitrarily slow to win the race. As for the problem of secure deletion, he's the solution I came up with. If anyone finds any problems with it, I'd like to know. But I've been using it for some time. I took the source to the GNU findutils 4.1, and added a "-delete" predicate. It is careful to make sure it only deletes what it should be deleting by doing a chdir() to the desired directory and then doing an unlink there. No chance of a linked directory causing problems. I also found and fixed a bug. Sorry I can't remember exactly what it was, it was about a year ago I made these changes. Using this for -delete, you will want to cd into the desired directory and then do a find from '.' after doing checking the current directory and making sure you are where you think you should be. For /tmp this is not necessary; but if you clean out users' .netscape/cache directories (which is what I did this for) then you need to be sure! diff -c -r findutils-4.1/find/defs.h findutils-4.1.NEW/find/defs.h *** findutils-4.1/find/defs.h Wed Nov 2 14:59:15 1994 --- findutils-4.1.NEW/find/defs.h Tue Nov 11 10:58:53 1997 *************** *** 249,254 **** --- 249,255 ---- boolean pred_cnewer P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); boolean pred_comma P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); boolean pred_ctime P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); + boolean pred_delete P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); boolean pred_empty P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); boolean pred_exec P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); boolean pred_false P_((char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)); diff -c -r findutils-4.1/find/find.c findutils-4.1.NEW/find/find.c *** findutils-4.1/find/find.c Wed Oct 12 16:21:11 1994 --- findutils-4.1.NEW/find/find.c Tue Nov 11 10:58:53 1997 *************** *** 105,111 **** boolean have_stat; /* The file being operated on, relative to the current directory. ! Used for stat, readlink, and opendir. */ char *rel_pathname; /* Length of current path. */ --- 105,111 ---- boolean have_stat; /* The file being operated on, relative to the current directory. ! Used for stat, readlink, remove, and opendir. */ char *rel_pathname; /* Length of current path. */ *************** *** 332,337 **** --- 332,339 ---- struct stat stat_buf; static dev_t root_dev; /* Device ID of current argument pathname. */ int i; + struct stat dir_buf; + int parent_desc; /* Assume it is a non-directory initially. */ stat_buf.st_mode = 0; *************** *** 390,398 **** apply_predicate (pathname, &stat_buf, eval_tree); if (stop_at_current_level == false) ! /* Scan directory on disk. */ ! process_dir (pathname, name, strlen (pathname), &stat_buf, parent); if (do_dir_first == false && curdepth >= mindepth) apply_predicate (pathname, &stat_buf, eval_tree); --- 392,440 ---- apply_predicate (pathname, &stat_buf, eval_tree); if (stop_at_current_level == false) ! { ! parent_desc = open (".", O_RDONLY); ! if (parent_desc < 0) ! error (1, errno, "%s", pathname); + if (chdir (name) < 0) + { + error (0, errno, "%s", pathname); + exit_status = 1; + close(parent_desc); + return; + } + + if ((*xstat) (".", &dir_buf) < 0) + error (1, errno, "%s", pathname); + + if (!S_ISDIR (dir_buf.st_mode) || dir_buf.st_ino != stat_buf.st_ino || + dir_buf.st_dev != stat_buf.st_dev) + error (1, 0, "Link changed: %s", pathname); + + /* Scan directory on disk. */ + process_dir (pathname, ".", strlen (pathname), &stat_buf, pathname); + + #ifndef HAVE_FCHDIR + if (!dereference) + { + if (chdir ("..") < 0) + error (1, errno, "%s", parent); + } + else + { + if (chdir (starting_dir) || chdir (parent)) + error (1, errno, "%s", parent); + } + #else + if (fchdir (parent_desc) < 0) + error (1, errno, "%s", parent); + #endif + close (parent_desc); + } + + rel_pathname = name; + if (do_dir_first == false && curdepth >= mindepth) apply_predicate (pathname, &stat_buf, eval_tree); *************** *** 454,466 **** cur_path_size = 0; cur_path = NULL; - if (strcmp (name, ".") && chdir (name) < 0) - { - error (0, errno, "%s", pathname); - exit_status = 1; - return; - } - for (namep = name_space; *namep; namep += file_len - pathname_len + 1) { /* Append this directory entry's name to the path being searched. */ --- 496,501 ---- *************** *** 495,521 **** mounted, which don't have Unix-like directory link counts. */ process_path (cur_path, cur_name, false, pathname); curdepth--; - } - - if (strcmp (name, ".")) - { - if (!dereference) - { - if (chdir ("..") < 0) - /* We could go back and do the next command-line arg instead, - maybe using longjmp. */ - error (1, errno, "%s", parent); - } - else - { - #ifndef HAVE_FCHDIR - if (chdir (starting_dir) || chdir (parent)) - error (1, errno, "%s", parent); - #else - if (fchdir (starting_desc) || chdir (parent)) - error (1, errno, "%s", parent); - #endif - } } if (cur_path) --- 530,535 ---- diff -c -r findutils-4.1/find/parser.c findutils-4.1.NEW/find/parser.c *** findutils-4.1/find/parser.c Wed Nov 2 14:59:19 1994 --- findutils-4.1.NEW/find/parser.c Tue Nov 11 10:58:54 1997 *************** *** 78,83 **** --- 78,84 ---- static boolean parse_comma P_((char *argv[], int *arg_ptr)); static boolean parse_ctime P_((char *argv[], int *arg_ptr)); static boolean parse_daystart P_((char *argv[], int *arg_ptr)); + static boolean parse_delete P_((char *argv[], int *arg_ptr)); static boolean parse_depth P_((char *argv[], int *arg_ptr)); static boolean parse_empty P_((char *argv[], int *arg_ptr)); static boolean parse_exec P_((char *argv[], int *arg_ptr)); *************** *** 176,181 **** --- 177,183 ---- #endif {"ctime", parse_ctime}, {"daystart", parse_daystart}, /* GNU */ + {"delete", parse_delete}, {"depth", parse_depth}, {"empty", parse_empty}, /* GNU */ {"exec", parse_exec}, *************** *** 427,432 **** --- 429,448 ---- } static boolean + parse_delete (argv, arg_ptr) + char *argv[]; + int *arg_ptr; + { + struct predicate *our_pred; + + our_pred = insert_primary (pred_delete); + our_pred->side_effects = true; + /* -delete implies -depth */ + do_dir_first = false; + return (true); + } + + static boolean parse_depth (argv, arg_ptr) char *argv[]; int *arg_ptr; *************** *** 611,617 **** ( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2\n"); printf ("\ EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2\n\ ! options (always true): -daystart -depth -follow --help\n\ -maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev\n\ tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N\n"); printf ("\ --- 627,633 ---- ( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2\n"); printf ("\ EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2\n\ ! options (always true): -daystart -delete -depth -follow --help\n\ -maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xdev\n\ tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N\n"); printf ("\ diff -c -r findutils-4.1/find/pred.c findutils-4.1.NEW/find/pred.c *** findutils-4.1/find/pred.c Wed Nov 2 14:59:23 1994 --- findutils-4.1.NEW/find/pred.c Tue Nov 11 10:58:54 1997 *************** *** 108,113 **** --- 108,114 ---- {pred_cnewer, "cnewer "}, {pred_comma, ", "}, {pred_ctime, "ctime "}, + {pred_delete, "delete "}, {pred_empty, "empty "}, {pred_exec, "exec "}, {pred_false, "false "}, *************** *** 376,381 **** --- 377,393 ---- break; } return (false); + } + + boolean + pred_delete (pathname, stat_buf, pred_ptr) + char *pathname; + struct stat *stat_buf; + struct predicate *pred_ptr; + { + if (strcmp (rel_pathname, ".")) + return (remove (rel_pathname)); + return (true); } boolean -- Douglas Siebert Director of Computing Facilities douglas-siebert@uiowa.edu Division of Mathematical Sciences, U of Iowa If at first you don't succeed, destroy all evidence that you tried.