findutils-4.4.2-selinux.diff 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. diff -purN findutils-4.4.2.orig/find/Makefile.am findutils-4.4.2/find/Makefile.am
  2. --- findutils-4.4.2.orig/find/Makefile.am 2009-05-16 15:17:01.000000000 +0000
  3. +++ findutils-4.4.2/find/Makefile.am 2010-04-25 03:58:33.762402367 +0000
  4. @@ -2,6 +2,7 @@ AUTOMAKE_OPTIONS = std-options
  5. localedir = $(datadir)/locale
  6. # noinst_PROGRAMS = regexprops
  7. # regexprops_SOURCES = regexprops.c
  8. +DEFS = @DEFS@ -I. -I$(srcdir) -I.. -DWITH_SELINUX
  9. noinst_LIBRARIES = libfindtools.a
  10. libfindtools_a_SOURCES = finddata.c fstype.c parser.c pred.c tree.c util.c
  11. @@ -26,7 +27,7 @@ endif
  12. EXTRA_DIST = defs.h $(man_MANS)
  13. INCLUDES = -I../gnulib/lib -I$(top_srcdir)/lib -I$(top_srcdir)/gnulib/lib -I../intl -DLOCALEDIR=\"$(localedir)\"
  14. -LDADD = ./libfindtools.a ../lib/libfind.a ../gnulib/lib/libgnulib.a @INTLLIBS@ @LIB_CLOCK_GETTIME@ @FINDLIBS@
  15. +LDADD = ./libfindtools.a ../lib/libfind.a ../gnulib/lib/libgnulib.a @INTLLIBS@ @LIB_CLOCK_GETTIME@ @FINDLIBS@ -lselinux
  16. man_MANS = find.1
  17. SUBDIRS = . testsuite
  18. diff -purN findutils-4.4.2.orig/find/Makefile.in findutils-4.4.2/find/Makefile.in
  19. --- findutils-4.4.2.orig/find/Makefile.in 2009-05-16 15:28:13.000000000 +0000
  20. +++ findutils-4.4.2/find/Makefile.in 2010-04-25 03:58:33.762402367 +0000
  21. @@ -275,7 +275,7 @@ CPP = @CPP@
  22. CPPFLAGS = @CPPFLAGS@
  23. CYGPATH_W = @CYGPATH_W@
  24. DEFAULT_ARG_SIZE = @DEFAULT_ARG_SIZE@
  25. -DEFS = @DEFS@
  26. +DEFS = @DEFS@ -I. -I$(srcdir) -I.. -DWITH_SELINUX
  27. DEPDIR = @DEPDIR@
  28. DIRENT_H = @DIRENT_H@
  29. ECHO_C = @ECHO_C@
  30. @@ -607,7 +607,7 @@ libfindtools_a_SOURCES = finddata.c fsty
  31. @WITH_FTS_TRUE@oldfind_SOURCES = find.c
  32. @WITH_FTS_FALSE@ftsfind_SOURCES = ftsfind.c
  33. EXTRA_DIST = defs.h $(man_MANS)
  34. -LDADD = ./libfindtools.a ../lib/libfind.a ../gnulib/lib/libgnulib.a @INTLLIBS@ @LIB_CLOCK_GETTIME@ @FINDLIBS@
  35. +LDADD = ./libfindtools.a ../lib/libfind.a ../gnulib/lib/libgnulib.a @INTLLIBS@ @LIB_CLOCK_GETTIME@ @FINDLIBS@ -lselinux
  36. man_MANS = find.1
  37. SUBDIRS = . testsuite
  38. all: all-recursive
  39. diff -purN findutils-4.4.2.orig/find/defs.h findutils-4.4.2/find/defs.h
  40. --- findutils-4.4.2.orig/find/defs.h 2009-05-16 15:17:01.000000000 +0000
  41. +++ findutils-4.4.2/find/defs.h 2010-04-25 03:58:33.762402367 +0000
  42. @@ -91,6 +91,9 @@ int get_statinfo PARAMS((const char *pat
  43. #define MODE_RWX (S_IXUSR | S_IXGRP | S_IXOTH | MODE_RW)
  44. #define MODE_ALL (S_ISUID | S_ISGID | S_ISVTX | MODE_RWX)
  45. +#ifdef WITH_SELINUX
  46. +#include <selinux/selinux.h>
  47. +#endif /*WITH_SELINUX*/
  48. struct predicate;
  49. struct options;
  50. @@ -315,6 +318,9 @@ struct predicate
  51. struct samefile_file_id samefileid; /* samefile */
  52. mode_t type; /* type */
  53. struct format_val printf_vec; /* printf fprintf fprint ls fls print0 fprint0 print */
  54. +#ifdef WITH_SELINUX
  55. + security_context_t scontext; /* scontext */
  56. +#endif /*WITH_SELINUX*/
  57. } args;
  58. /* The next predicate in the user input sequence,
  59. @@ -459,7 +465,9 @@ PREDICATEFUNCTION pred_used;
  60. PREDICATEFUNCTION pred_user;
  61. PREDICATEFUNCTION pred_writable;
  62. PREDICATEFUNCTION pred_xtype;
  63. -
  64. +#ifdef WITH_SELINUX
  65. +PREDICATEFUNCTION pred_scontext;
  66. +#endif /* WITH_SELINUX */
  67. int launch PARAMS((const struct buildcmd_control *ctl,
  68. @@ -605,10 +613,13 @@ struct options
  69. */
  70. unsigned short optimisation_level;
  71. -
  72. /* How should we quote filenames in error messages and so forth?
  73. */
  74. enum quoting_style err_quoting_style;
  75. +
  76. +#ifdef WITH_SELINUX
  77. + int (*x_getfilecon)();
  78. +#endif /* WITH_SELINUX */
  79. };
  80. extern struct options options;
  81. diff -purN findutils-4.4.2.orig/find/find.1 findutils-4.4.2/find/find.1
  82. --- findutils-4.4.2.orig/find/find.1 2009-05-16 15:17:01.000000000 +0000
  83. +++ findutils-4.4.2/find/find.1 2010-04-25 03:58:33.762402367 +0000
  84. @@ -934,6 +934,10 @@ checks the type of the file that
  85. .B \-type
  86. does not check.
  87. +.IP "\-context \fIscontext\fR"
  88. +.IP "\--context \fIscontext\fR"
  89. +(SELinux only) File has the security context \fIscontext\fR.
  90. +
  91. .SS ACTIONS
  92. .IP "\-delete\fR"
  93. Delete files; true if removal succeeded. If the removal failed, an
  94. @@ -1354,6 +1358,8 @@ File's type (like in
  95. U=unknown type (shouldn't happen)
  96. .IP %Y
  97. File's type (like %y), plus follow symlinks: L=loop, N=nonexistent
  98. +.IP %Z
  99. +(SELinux only) file's security context.
  100. .PP
  101. A `%' character followed by any other character is discarded, but the
  102. other character is printed (don't rely on this, as further format
  103. diff -purN findutils-4.4.2.orig/find/find.c findutils-4.4.2/find/find.c
  104. --- findutils-4.4.2.orig/find/find.c 2009-05-16 15:17:01.000000000 +0000
  105. +++ findutils-4.4.2/find/find.c 2010-04-25 03:58:33.764899827 +0000
  106. @@ -1270,7 +1270,7 @@ process_path (char *pathname, char *name
  107. static void
  108. process_dir (char *pathname, char *name, int pathlen, const struct stat *statp, char *parent)
  109. {
  110. - int subdirs_left; /* Number of unexamined subdirs in PATHNAME. */
  111. + int subdirs_left=0; /* Number of unexamined subdirs in PATHNAME. */
  112. boolean subdirs_unreliable; /* if true, cannot use dir link count as subdir limif (if false, it may STILL be unreliable) */
  113. unsigned int idx; /* Which entry are we on? */
  114. struct stat stat_buf;
  115. diff -purN findutils-4.4.2.orig/find/parser.c findutils-4.4.2/find/parser.c
  116. --- findutils-4.4.2.orig/find/parser.c 2009-05-16 15:17:01.000000000 +0000
  117. +++ findutils-4.4.2/find/parser.c 2010-04-25 03:58:33.764899827 +0000
  118. @@ -53,6 +53,13 @@
  119. #include <unistd.h>
  120. #include <sys/stat.h>
  121. +#ifdef WITH_SELINUX
  122. +#include <selinux/selinux.h>
  123. +int optionh_getfilecon(const char *name, security_context_t *p);
  124. +int optionl_getfilecon(const char *name, security_context_t *p);
  125. +int optionp_getfilecon(const char *name, security_context_t *p);
  126. +#endif /*WITH_SELINUX*/
  127. +
  128. #if ENABLE_NLS
  129. # include <libintl.h>
  130. # define _(Text) gettext (Text)
  131. @@ -155,6 +162,9 @@ static boolean parse_noignore_race PARAM
  132. static boolean parse_warn PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
  133. static boolean parse_xtype PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
  134. static boolean parse_quit PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
  135. +#ifdef WITH_SELINUX
  136. +static boolean parse_scontext PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
  137. +#endif /*WITH_SELINUX*/
  138. boolean parse_print PARAMS((const struct parser_table*, char *argv[], int *arg_ptr));
  139. @@ -340,6 +350,8 @@ static struct parser_table const parse_t
  140. {ARG_TEST, "-help", parse_help, NULL}, /* GNU */
  141. {ARG_TEST, "version", parse_version, NULL}, /* GNU */
  142. {ARG_TEST, "-version", parse_version, NULL}, /* GNU */
  143. + {ARG_TEST, "context", parse_scontext, pred_scontext}, /* SELinux */
  144. + {ARG_TEST, "-context", parse_scontext, pred_scontext}, /* SELinux */
  145. {0, 0, 0, 0}
  146. };
  147. @@ -451,10 +463,16 @@ set_follow_state(enum SymlinkOption opt)
  148. case SYMLINK_ALWAYS_DEREF: /* -L */
  149. options.xstat = optionl_stat;
  150. options.no_leaf_check = true;
  151. +#ifdef WITH_SELINUX
  152. + options.x_getfilecon = optionl_getfilecon;
  153. +#endif /* WITH_SELINUX */
  154. break;
  155. case SYMLINK_NEVER_DEREF: /* -P (default) */
  156. options.xstat = optionp_stat;
  157. +#ifdef WITH_SELINUX
  158. + options.x_getfilecon = optionp_getfilecon;
  159. +#endif /* WITH_SELINUX */
  160. /* Can't turn no_leaf_check off because the user might have specified
  161. * -noleaf anyway
  162. */
  163. @@ -463,6 +481,9 @@ set_follow_state(enum SymlinkOption opt)
  164. case SYMLINK_DEREF_ARGSONLY: /* -H */
  165. options.xstat = optionh_stat;
  166. options.no_leaf_check = true;
  167. +#ifdef WITH_SELINUX
  168. + options.x_getfilecon = optionh_getfilecon;
  169. +#endif /* WITH_SELINUX */
  170. }
  171. }
  172. options.symlink_handling = opt;
  173. @@ -670,6 +691,94 @@ collect_arg_stat_info(char **argv, int *
  174. The predicate structure is updated with the new information. */
  175. +#ifdef WITH_SELINUX
  176. +
  177. +static int
  178. +fallback_getfilecon(const char *name, security_context_t *p, int prev_rv)
  179. +{
  180. + /* Our original getfilecon() call failed. Perhaps we can't follow a
  181. + * symbolic link. If that might be the problem, lgetfilecon() the link.
  182. + * Otherwise, admit defeat.
  183. + */
  184. + switch (errno)
  185. + {
  186. + case ENOENT:
  187. + case ENOTDIR:
  188. +#ifdef DEBUG_STAT
  189. + fprintf(stderr, "fallback_getfilecon(): getfilecon(%s) failed; falling back on lgetfilecon()\n", name);
  190. +#endif
  191. + return lgetfilecon(name, p);
  192. +
  193. + case EACCES:
  194. + case EIO:
  195. + case ELOOP:
  196. + case ENAMETOOLONG:
  197. +#ifdef EOVERFLOW
  198. + case EOVERFLOW: /* EOVERFLOW is not #defined on UNICOS. */
  199. +#endif
  200. + default:
  201. + return prev_rv;
  202. + }
  203. +}
  204. +
  205. +
  206. +/* optionh_getfilecon() implements the getfilecon operation when the
  207. + * -H option is in effect.
  208. + *
  209. + * If the item to be examined is a command-line argument, we follow
  210. + * symbolic links. If the getfilecon() call fails on the command-line
  211. + * item, we fall back on the properties of the symbolic link.
  212. + *
  213. + * If the item to be examined is not a command-line argument, we
  214. + * examine the link itself.
  215. + */
  216. +int
  217. +optionh_getfilecon(const char *name, security_context_t *p)
  218. +{
  219. + if (0 == state.curdepth)
  220. + {
  221. + /* This file is from the command line; deference the link (if it
  222. + * is a link).
  223. + */
  224. + int rv = getfilecon(name, p);
  225. + if (0 == rv)
  226. + return 0; /* success */
  227. + else
  228. + return fallback_getfilecon(name, p, rv);
  229. + }
  230. + else
  231. + {
  232. + /* Not a file on the command line; do not derefernce the link.
  233. + */
  234. + return lgetfilecon(name, p);
  235. + }
  236. +}
  237. +
  238. +/* optionl_getfilecon() implements the getfilecon operation when the
  239. + * -L option is in effect. That option makes us examine the thing the
  240. + * symbolic link points to, not the symbolic link itself.
  241. + */
  242. +int
  243. +optionl_getfilecon(const char *name, security_context_t *p)
  244. +{
  245. + int rv = getfilecon(name, p);
  246. + if (0 == rv)
  247. + return 0; /* normal case. */
  248. + else
  249. + return fallback_getfilecon(name, p, rv);
  250. +}
  251. +
  252. +/* optionp_getfilecon() implements the stat operation when the -P
  253. + * option is in effect (this is also the default). That option makes
  254. + * us examine the symbolic link itself, not the thing it points to.
  255. + */
  256. +int
  257. +optionp_getfilecon(const char *name, security_context_t *p)
  258. +{
  259. + return lgetfilecon(name, p);
  260. +}
  261. +#endif /* WITH_SELINUX */
  262. +
  263. static boolean
  264. parse_and (const struct parser_table* entry, char **argv, int *arg_ptr)
  265. @@ -1128,6 +1237,10 @@ tests (N can be +N or -N or N): -amin N
  266. -readable -writable -executable\n\
  267. -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N\n\
  268. -used N -user NAME -xtype [bcdpfls]\n"));
  269. +#ifdef WITH_SELINUX
  270. + puts (_("\
  271. + -context CONTEXT\n"));
  272. +#endif /*WITH_SELINUX*/
  273. puts (_("\
  274. actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print \n\
  275. -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit\n\
  276. @@ -2552,6 +2665,29 @@ parse_version (const struct parser_table
  277. exit (0);
  278. }
  279. +#ifdef WITH_SELINUX
  280. +
  281. +static boolean
  282. +parse_scontext ( const struct parser_table* entry, char **argv, int *arg_ptr)
  283. +{
  284. + struct predicate *our_pred;
  285. +
  286. + if ( (argv == NULL) || (argv[*arg_ptr] == NULL) )
  287. + return( false );
  288. +
  289. + our_pred = insert_primary(entry);
  290. + our_pred->need_stat = false;
  291. +#ifdef DEBUG
  292. + our_pred->p_name = find_pred_name (pred_scontext);
  293. +#endif /*DEBUG*/
  294. + our_pred->args.scontext = argv[*arg_ptr];;
  295. +
  296. + (*arg_ptr)++;
  297. + return( true );
  298. +}
  299. +
  300. +#endif /*WITH_SELINUX*/
  301. +
  302. static boolean
  303. parse_xdev (const struct parser_table* entry, char **argv, int *arg_ptr)
  304. {
  305. @@ -2803,7 +2939,11 @@ insert_fprintf (struct format_val *vec,
  306. if (*scan2 == '.')
  307. for (scan2++; ISDIGIT (*scan2); scan2++)
  308. /* Do nothing. */ ;
  309. +#ifdef WITH_SELINUX
  310. + if (strchr ("abcdDfFgGhHiklmMnpPsStuUyYZ", *scan2))
  311. +#else
  312. if (strchr ("abcdDfFgGhHiklmMnpPsStuUyY", *scan2))
  313. +#endif
  314. {
  315. segmentp = make_segment (segmentp, format, scan2 - format,
  316. KIND_FORMAT, *scan2, 0,
  317. diff -purN findutils-4.4.2.orig/find/pred.c findutils-4.4.2/find/pred.c
  318. --- findutils-4.4.2.orig/find/pred.c 2009-05-16 15:17:01.000000000 +0000
  319. +++ findutils-4.4.2/find/pred.c 2010-04-25 03:58:33.764899827 +0000
  320. @@ -48,6 +48,14 @@
  321. #include "error.h"
  322. #include "verify.h"
  323. +#ifdef WITH_SELINUX
  324. +#include <selinux/selinux.h>
  325. +#endif /*WITH_SELINUX*/
  326. +
  327. +#ifndef FNM_CASEFOLD
  328. +#define FNM_CASEFOLD (1<<4)
  329. +#endif /*FNM_CASEFOLD*/
  330. +
  331. #if ENABLE_NLS
  332. # include <libintl.h>
  333. # define _(Text) gettext (Text)
  334. @@ -230,6 +238,9 @@ struct pred_assoc pred_table[] =
  335. {pred_user, "user "},
  336. {pred_writable, "writable "},
  337. {pred_xtype, "xtype "},
  338. +#ifdef WITH_SELINUX
  339. + {pred_scontext, "context"},
  340. +#endif /*WITH_SELINUX*/
  341. {0, "none "}
  342. };
  343. #endif
  344. @@ -1054,6 +1065,26 @@ do_fprintf(struct format_val *dest,
  345. mode_to_filetype(stat_buf->st_mode & S_IFMT));
  346. }
  347. break;
  348. +#ifdef WITH_SELINUX
  349. + case 'Z': /* SELinux security context */
  350. + {
  351. + security_context_t scontext;
  352. + int rv;
  353. + rv = (*options.x_getfilecon)(state.rel_pathname, &scontext);
  354. +
  355. + if ( rv < 0 ) {
  356. + fprintf(stderr, "getfileconf(%s): %s",
  357. + pathname, strerror(errno));
  358. + fflush(stderr);
  359. + }
  360. + else {
  361. + segment->text[segment->text_len] = 's';
  362. + checked_fprintf (dest, segment->text, scontext);
  363. + freecon(scontext);
  364. + }
  365. + }
  366. + break ;
  367. +#endif /* WITH_SELINUX */
  368. }
  369. /* end of KIND_FORMAT case */
  370. break;
  371. @@ -1844,6 +1875,31 @@ pred_xtype (const char *pathname, struct
  372. */
  373. return (pred_type (pathname, &sbuf, pred_ptr));
  374. }
  375. +
  376. +
  377. +#ifdef WITH_SELINUX
  378. +
  379. +boolean
  380. +pred_scontext (const char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
  381. +{
  382. + int rv;
  383. + security_context_t scontext;
  384. +
  385. + rv = (* options.x_getfilecon)(state.rel_pathname, &scontext);
  386. +
  387. + if ( rv < 0 ) {
  388. + (void) fprintf(stderr, "getfilecon(%s): %s\n", pathname, strerror(errno));
  389. + (void) fflush(stderr);
  390. + return ( false );
  391. + }
  392. +
  393. + rv = (fnmatch(pred_ptr->args.scontext, scontext,0)==0);
  394. + freecon(scontext);
  395. + return rv;
  396. +}
  397. +
  398. +#endif /*WITH_SELINUX*/
  399. +
  400. /* 1) fork to get a child; parent remembers the child pid
  401. 2) child execs the command requested
  402. diff -purN findutils-4.4.2.orig/find/tree.c findutils-4.4.2/find/tree.c
  403. --- findutils-4.4.2.orig/find/tree.c 2009-05-16 15:17:01.000000000 +0000
  404. +++ findutils-4.4.2/find/tree.c 2010-04-25 03:58:33.764899827 +0000
  405. @@ -1195,6 +1195,10 @@ build_expression_tree(int argc, char *ar
  406. const struct parser_table *entry_close, *entry_print, *entry_open;
  407. int i, oldi;
  408. +#ifdef WITH_SELINUX
  409. + int is_selinux_enabled_flag = is_selinux_enabled()>0;
  410. +#endif /* WITH_SELINUX */
  411. +
  412. predicates = NULL;
  413. /* Find where in ARGV the predicates begin by skipping the list of
  414. @@ -1230,6 +1234,16 @@ build_expression_tree(int argc, char *ar
  415. }
  416. predicate_name = argv[i];
  417. +
  418. +#ifdef WITH_SELINUX
  419. + if (! is_selinux_enabled_flag) {
  420. + if ((strncmp(predicate_name,"-context",strlen("-context"))==0) ||
  421. + (strncmp(predicate_name,"--context",strlen("--context"))==0)) {
  422. + error (1, 0,_("Error: invalid predicate %s: the kernel is not selinux-enabled.\n"),predicate_name);
  423. + }
  424. + }
  425. +#endif
  426. +
  427. parse_entry = find_parser (predicate_name);
  428. if (parse_entry == NULL)
  429. {
  430. @@ -1434,6 +1448,9 @@ get_new_pred (const struct parser_table
  431. last_pred->need_stat = true;
  432. last_pred->need_type = true;
  433. last_pred->args.str = NULL;
  434. +#ifdef WITH_SELINUX
  435. + last_pred->args.scontext = NULL;
  436. +#endif
  437. last_pred->pred_next = NULL;
  438. last_pred->pred_left = NULL;
  439. last_pred->pred_right = NULL;