auditfilter.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /* auditfilter.c -- filtering of audit events
  2. *
  3. * Copyright 2003-2004 Red Hat, Inc.
  4. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  5. * Copyright 2005 IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/audit.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/fs.h>
  26. #include <linux/namei.h>
  27. #include <linux/netlink.h>
  28. #include <linux/sched.h>
  29. #include <linux/inotify.h>
  30. #include <linux/selinux.h>
  31. #include "audit.h"
  32. /*
  33. * Locking model:
  34. *
  35. * audit_filter_mutex:
  36. * Synchronizes writes and blocking reads of audit's filterlist
  37. * data. Rcu is used to traverse the filterlist and access
  38. * contents of structs audit_entry, audit_watch and opaque
  39. * selinux rules during filtering. If modified, these structures
  40. * must be copied and replace their counterparts in the filterlist.
  41. * An audit_parent struct is not accessed during filtering, so may
  42. * be written directly provided audit_filter_mutex is held.
  43. */
  44. /*
  45. * Reference counting:
  46. *
  47. * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
  48. * event. Each audit_watch holds a reference to its associated parent.
  49. *
  50. * audit_watch: if added to lists, lifetime is from audit_init_watch() to
  51. * audit_remove_watch(). Additionally, an audit_watch may exist
  52. * temporarily to assist in searching existing filter data. Each
  53. * audit_krule holds a reference to its associated watch.
  54. */
  55. struct audit_parent {
  56. struct list_head ilist; /* entry in inotify registration list */
  57. struct list_head watches; /* associated watches */
  58. struct inotify_watch wdata; /* inotify watch data */
  59. unsigned flags; /* status flags */
  60. };
  61. /*
  62. * audit_parent status flags:
  63. *
  64. * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
  65. * a filesystem event to ensure we're adding audit watches to a valid parent.
  66. * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
  67. * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
  68. * we can receive while holding nameidata.
  69. */
  70. #define AUDIT_PARENT_INVALID 0x001
  71. /* Audit filter lists, defined in <linux/audit.h> */
  72. struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  73. LIST_HEAD_INIT(audit_filter_list[0]),
  74. LIST_HEAD_INIT(audit_filter_list[1]),
  75. LIST_HEAD_INIT(audit_filter_list[2]),
  76. LIST_HEAD_INIT(audit_filter_list[3]),
  77. LIST_HEAD_INIT(audit_filter_list[4]),
  78. LIST_HEAD_INIT(audit_filter_list[5]),
  79. #if AUDIT_NR_FILTERS != 6
  80. #error Fix audit_filter_list initialiser
  81. #endif
  82. };
  83. static DEFINE_MUTEX(audit_filter_mutex);
  84. /* Inotify handle */
  85. extern struct inotify_handle *audit_ih;
  86. /* Inotify events we care about. */
  87. #define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
  88. void audit_free_parent(struct inotify_watch *i_watch)
  89. {
  90. struct audit_parent *parent;
  91. parent = container_of(i_watch, struct audit_parent, wdata);
  92. WARN_ON(!list_empty(&parent->watches));
  93. kfree(parent);
  94. }
  95. static inline void audit_get_watch(struct audit_watch *watch)
  96. {
  97. atomic_inc(&watch->count);
  98. }
  99. static void audit_put_watch(struct audit_watch *watch)
  100. {
  101. if (atomic_dec_and_test(&watch->count)) {
  102. WARN_ON(watch->parent);
  103. WARN_ON(!list_empty(&watch->rules));
  104. kfree(watch->path);
  105. kfree(watch);
  106. }
  107. }
  108. static void audit_remove_watch(struct audit_watch *watch)
  109. {
  110. list_del(&watch->wlist);
  111. put_inotify_watch(&watch->parent->wdata);
  112. watch->parent = NULL;
  113. audit_put_watch(watch); /* match initial get */
  114. }
  115. static inline void audit_free_rule(struct audit_entry *e)
  116. {
  117. int i;
  118. /* some rules don't have associated watches */
  119. if (e->rule.watch)
  120. audit_put_watch(e->rule.watch);
  121. if (e->rule.fields)
  122. for (i = 0; i < e->rule.field_count; i++) {
  123. struct audit_field *f = &e->rule.fields[i];
  124. kfree(f->se_str);
  125. selinux_audit_rule_free(f->se_rule);
  126. }
  127. kfree(e->rule.fields);
  128. kfree(e->rule.filterkey);
  129. kfree(e);
  130. }
  131. static inline void audit_free_rule_rcu(struct rcu_head *head)
  132. {
  133. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  134. audit_free_rule(e);
  135. }
  136. /* Initialize a parent watch entry. */
  137. static struct audit_parent *audit_init_parent(struct nameidata *ndp)
  138. {
  139. struct audit_parent *parent;
  140. s32 wd;
  141. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  142. if (unlikely(!parent))
  143. return ERR_PTR(-ENOMEM);
  144. INIT_LIST_HEAD(&parent->watches);
  145. parent->flags = 0;
  146. inotify_init_watch(&parent->wdata);
  147. /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
  148. get_inotify_watch(&parent->wdata);
  149. wd = inotify_add_watch(audit_ih, &parent->wdata, ndp->dentry->d_inode,
  150. AUDIT_IN_WATCH);
  151. if (wd < 0) {
  152. audit_free_parent(&parent->wdata);
  153. return ERR_PTR(wd);
  154. }
  155. return parent;
  156. }
  157. /* Initialize a watch entry. */
  158. static struct audit_watch *audit_init_watch(char *path)
  159. {
  160. struct audit_watch *watch;
  161. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  162. if (unlikely(!watch))
  163. return ERR_PTR(-ENOMEM);
  164. INIT_LIST_HEAD(&watch->rules);
  165. atomic_set(&watch->count, 1);
  166. watch->path = path;
  167. watch->dev = (dev_t)-1;
  168. watch->ino = (unsigned long)-1;
  169. return watch;
  170. }
  171. /* Initialize an audit filterlist entry. */
  172. static inline struct audit_entry *audit_init_entry(u32 field_count)
  173. {
  174. struct audit_entry *entry;
  175. struct audit_field *fields;
  176. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  177. if (unlikely(!entry))
  178. return NULL;
  179. fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
  180. if (unlikely(!fields)) {
  181. kfree(entry);
  182. return NULL;
  183. }
  184. entry->rule.fields = fields;
  185. return entry;
  186. }
  187. /* Unpack a filter field's string representation from user-space
  188. * buffer. */
  189. static char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
  190. {
  191. char *str;
  192. if (!*bufp || (len == 0) || (len > *remain))
  193. return ERR_PTR(-EINVAL);
  194. /* Of the currently implemented string fields, PATH_MAX
  195. * defines the longest valid length.
  196. */
  197. if (len > PATH_MAX)
  198. return ERR_PTR(-ENAMETOOLONG);
  199. str = kmalloc(len + 1, GFP_KERNEL);
  200. if (unlikely(!str))
  201. return ERR_PTR(-ENOMEM);
  202. memcpy(str, *bufp, len);
  203. str[len] = 0;
  204. *bufp += len;
  205. *remain -= len;
  206. return str;
  207. }
  208. /* Translate an inode field to kernel respresentation. */
  209. static inline int audit_to_inode(struct audit_krule *krule,
  210. struct audit_field *f)
  211. {
  212. if (krule->listnr != AUDIT_FILTER_EXIT ||
  213. krule->watch || krule->inode_f)
  214. return -EINVAL;
  215. krule->inode_f = f;
  216. return 0;
  217. }
  218. /* Translate a watch string to kernel respresentation. */
  219. static int audit_to_watch(struct audit_krule *krule, char *path, int len,
  220. u32 op)
  221. {
  222. struct audit_watch *watch;
  223. if (!audit_ih)
  224. return -EOPNOTSUPP;
  225. if (path[0] != '/' || path[len-1] == '/' ||
  226. krule->listnr != AUDIT_FILTER_EXIT ||
  227. op & ~AUDIT_EQUAL ||
  228. krule->inode_f || krule->watch) /* 1 inode # per rule, for hash */
  229. return -EINVAL;
  230. watch = audit_init_watch(path);
  231. if (unlikely(IS_ERR(watch)))
  232. return PTR_ERR(watch);
  233. audit_get_watch(watch);
  234. krule->watch = watch;
  235. return 0;
  236. }
  237. static __u32 *classes[AUDIT_SYSCALL_CLASSES];
  238. int __init audit_register_class(int class, unsigned *list)
  239. {
  240. __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
  241. if (!p)
  242. return -ENOMEM;
  243. while (*list != ~0U) {
  244. unsigned n = *list++;
  245. if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
  246. kfree(p);
  247. return -EINVAL;
  248. }
  249. p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
  250. }
  251. if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
  252. kfree(p);
  253. return -EINVAL;
  254. }
  255. classes[class] = p;
  256. return 0;
  257. }
  258. int audit_match_class(int class, unsigned syscall)
  259. {
  260. if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
  261. return 0;
  262. if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
  263. return 0;
  264. return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
  265. }
  266. #ifdef CONFIG_AUDITSYSCALL
  267. static inline int audit_match_class_bits(int class, u32 *mask)
  268. {
  269. int i;
  270. if (classes[class]) {
  271. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  272. if (mask[i] & classes[class][i])
  273. return 0;
  274. }
  275. return 1;
  276. }
  277. static int audit_match_signal(struct audit_entry *entry)
  278. {
  279. struct audit_field *arch = entry->rule.arch_f;
  280. if (!arch) {
  281. /* When arch is unspecified, we must check both masks on biarch
  282. * as syscall number alone is ambiguous. */
  283. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  284. entry->rule.mask) &&
  285. audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  286. entry->rule.mask));
  287. }
  288. switch(audit_classify_arch(arch->val)) {
  289. case 0: /* native */
  290. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  291. entry->rule.mask));
  292. case 1: /* 32bit on biarch */
  293. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  294. entry->rule.mask));
  295. default:
  296. return 1;
  297. }
  298. }
  299. #endif
  300. /* Common user-space to kernel rule translation. */
  301. static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
  302. {
  303. unsigned listnr;
  304. struct audit_entry *entry;
  305. int i, err;
  306. err = -EINVAL;
  307. listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
  308. switch(listnr) {
  309. default:
  310. goto exit_err;
  311. case AUDIT_FILTER_USER:
  312. case AUDIT_FILTER_TYPE:
  313. #ifdef CONFIG_AUDITSYSCALL
  314. case AUDIT_FILTER_ENTRY:
  315. case AUDIT_FILTER_EXIT:
  316. case AUDIT_FILTER_TASK:
  317. #endif
  318. ;
  319. }
  320. if (unlikely(rule->action == AUDIT_POSSIBLE)) {
  321. printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
  322. goto exit_err;
  323. }
  324. if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
  325. goto exit_err;
  326. if (rule->field_count > AUDIT_MAX_FIELDS)
  327. goto exit_err;
  328. err = -ENOMEM;
  329. entry = audit_init_entry(rule->field_count);
  330. if (!entry)
  331. goto exit_err;
  332. entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
  333. entry->rule.listnr = listnr;
  334. entry->rule.action = rule->action;
  335. entry->rule.field_count = rule->field_count;
  336. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  337. entry->rule.mask[i] = rule->mask[i];
  338. for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
  339. int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
  340. __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
  341. __u32 *class;
  342. if (!(*p & AUDIT_BIT(bit)))
  343. continue;
  344. *p &= ~AUDIT_BIT(bit);
  345. class = classes[i];
  346. if (class) {
  347. int j;
  348. for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
  349. entry->rule.mask[j] |= class[j];
  350. }
  351. }
  352. return entry;
  353. exit_err:
  354. return ERR_PTR(err);
  355. }
  356. /* Translate struct audit_rule to kernel's rule respresentation.
  357. * Exists for backward compatibility with userspace. */
  358. static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
  359. {
  360. struct audit_entry *entry;
  361. struct audit_field *f;
  362. int err = 0;
  363. int i;
  364. entry = audit_to_entry_common(rule);
  365. if (IS_ERR(entry))
  366. goto exit_nofree;
  367. for (i = 0; i < rule->field_count; i++) {
  368. struct audit_field *f = &entry->rule.fields[i];
  369. f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
  370. f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
  371. f->val = rule->values[i];
  372. err = -EINVAL;
  373. switch(f->type) {
  374. default:
  375. goto exit_free;
  376. case AUDIT_PID:
  377. case AUDIT_UID:
  378. case AUDIT_EUID:
  379. case AUDIT_SUID:
  380. case AUDIT_FSUID:
  381. case AUDIT_GID:
  382. case AUDIT_EGID:
  383. case AUDIT_SGID:
  384. case AUDIT_FSGID:
  385. case AUDIT_LOGINUID:
  386. case AUDIT_PERS:
  387. case AUDIT_MSGTYPE:
  388. case AUDIT_PPID:
  389. case AUDIT_DEVMAJOR:
  390. case AUDIT_DEVMINOR:
  391. case AUDIT_EXIT:
  392. case AUDIT_SUCCESS:
  393. /* bit ops are only useful on syscall args */
  394. if (f->op == AUDIT_BIT_MASK ||
  395. f->op == AUDIT_BIT_TEST) {
  396. err = -EINVAL;
  397. goto exit_free;
  398. }
  399. break;
  400. case AUDIT_ARG0:
  401. case AUDIT_ARG1:
  402. case AUDIT_ARG2:
  403. case AUDIT_ARG3:
  404. break;
  405. /* arch is only allowed to be = or != */
  406. case AUDIT_ARCH:
  407. if ((f->op != AUDIT_NOT_EQUAL) && (f->op != AUDIT_EQUAL)
  408. && (f->op != AUDIT_NEGATE) && (f->op)) {
  409. err = -EINVAL;
  410. goto exit_free;
  411. }
  412. entry->rule.arch_f = f;
  413. break;
  414. case AUDIT_PERM:
  415. if (f->val & ~15)
  416. goto exit_free;
  417. break;
  418. case AUDIT_INODE:
  419. err = audit_to_inode(&entry->rule, f);
  420. if (err)
  421. goto exit_free;
  422. break;
  423. }
  424. entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
  425. /* Support for legacy operators where
  426. * AUDIT_NEGATE bit signifies != and otherwise assumes == */
  427. if (f->op & AUDIT_NEGATE)
  428. f->op = AUDIT_NOT_EQUAL;
  429. else if (!f->op)
  430. f->op = AUDIT_EQUAL;
  431. else if (f->op == AUDIT_OPERATORS) {
  432. err = -EINVAL;
  433. goto exit_free;
  434. }
  435. }
  436. f = entry->rule.inode_f;
  437. if (f) {
  438. switch(f->op) {
  439. case AUDIT_NOT_EQUAL:
  440. entry->rule.inode_f = NULL;
  441. case AUDIT_EQUAL:
  442. break;
  443. default:
  444. err = -EINVAL;
  445. goto exit_free;
  446. }
  447. }
  448. exit_nofree:
  449. return entry;
  450. exit_free:
  451. audit_free_rule(entry);
  452. return ERR_PTR(err);
  453. }
  454. /* Translate struct audit_rule_data to kernel's rule respresentation. */
  455. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  456. size_t datasz)
  457. {
  458. int err = 0;
  459. struct audit_entry *entry;
  460. struct audit_field *f;
  461. void *bufp;
  462. size_t remain = datasz - sizeof(struct audit_rule_data);
  463. int i;
  464. char *str;
  465. entry = audit_to_entry_common((struct audit_rule *)data);
  466. if (IS_ERR(entry))
  467. goto exit_nofree;
  468. bufp = data->buf;
  469. entry->rule.vers_ops = 2;
  470. for (i = 0; i < data->field_count; i++) {
  471. struct audit_field *f = &entry->rule.fields[i];
  472. err = -EINVAL;
  473. if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
  474. data->fieldflags[i] & ~AUDIT_OPERATORS)
  475. goto exit_free;
  476. f->op = data->fieldflags[i] & AUDIT_OPERATORS;
  477. f->type = data->fields[i];
  478. f->val = data->values[i];
  479. f->se_str = NULL;
  480. f->se_rule = NULL;
  481. switch(f->type) {
  482. case AUDIT_PID:
  483. case AUDIT_UID:
  484. case AUDIT_EUID:
  485. case AUDIT_SUID:
  486. case AUDIT_FSUID:
  487. case AUDIT_GID:
  488. case AUDIT_EGID:
  489. case AUDIT_SGID:
  490. case AUDIT_FSGID:
  491. case AUDIT_LOGINUID:
  492. case AUDIT_PERS:
  493. case AUDIT_MSGTYPE:
  494. case AUDIT_PPID:
  495. case AUDIT_DEVMAJOR:
  496. case AUDIT_DEVMINOR:
  497. case AUDIT_EXIT:
  498. case AUDIT_SUCCESS:
  499. case AUDIT_ARG0:
  500. case AUDIT_ARG1:
  501. case AUDIT_ARG2:
  502. case AUDIT_ARG3:
  503. break;
  504. case AUDIT_ARCH:
  505. entry->rule.arch_f = f;
  506. break;
  507. case AUDIT_SUBJ_USER:
  508. case AUDIT_SUBJ_ROLE:
  509. case AUDIT_SUBJ_TYPE:
  510. case AUDIT_SUBJ_SEN:
  511. case AUDIT_SUBJ_CLR:
  512. case AUDIT_OBJ_USER:
  513. case AUDIT_OBJ_ROLE:
  514. case AUDIT_OBJ_TYPE:
  515. case AUDIT_OBJ_LEV_LOW:
  516. case AUDIT_OBJ_LEV_HIGH:
  517. str = audit_unpack_string(&bufp, &remain, f->val);
  518. if (IS_ERR(str))
  519. goto exit_free;
  520. entry->rule.buflen += f->val;
  521. err = selinux_audit_rule_init(f->type, f->op, str,
  522. &f->se_rule);
  523. /* Keep currently invalid fields around in case they
  524. * become valid after a policy reload. */
  525. if (err == -EINVAL) {
  526. printk(KERN_WARNING "audit rule for selinux "
  527. "\'%s\' is invalid\n", str);
  528. err = 0;
  529. }
  530. if (err) {
  531. kfree(str);
  532. goto exit_free;
  533. } else
  534. f->se_str = str;
  535. break;
  536. case AUDIT_WATCH:
  537. str = audit_unpack_string(&bufp, &remain, f->val);
  538. if (IS_ERR(str))
  539. goto exit_free;
  540. entry->rule.buflen += f->val;
  541. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  542. if (err) {
  543. kfree(str);
  544. goto exit_free;
  545. }
  546. break;
  547. case AUDIT_INODE:
  548. err = audit_to_inode(&entry->rule, f);
  549. if (err)
  550. goto exit_free;
  551. break;
  552. case AUDIT_FILTERKEY:
  553. err = -EINVAL;
  554. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  555. goto exit_free;
  556. str = audit_unpack_string(&bufp, &remain, f->val);
  557. if (IS_ERR(str))
  558. goto exit_free;
  559. entry->rule.buflen += f->val;
  560. entry->rule.filterkey = str;
  561. break;
  562. case AUDIT_PERM:
  563. if (f->val & ~15)
  564. goto exit_free;
  565. break;
  566. default:
  567. goto exit_free;
  568. }
  569. }
  570. f = entry->rule.inode_f;
  571. if (f) {
  572. switch(f->op) {
  573. case AUDIT_NOT_EQUAL:
  574. entry->rule.inode_f = NULL;
  575. case AUDIT_EQUAL:
  576. break;
  577. default:
  578. err = -EINVAL;
  579. goto exit_free;
  580. }
  581. }
  582. exit_nofree:
  583. return entry;
  584. exit_free:
  585. audit_free_rule(entry);
  586. return ERR_PTR(err);
  587. }
  588. /* Pack a filter field's string representation into data block. */
  589. static inline size_t audit_pack_string(void **bufp, char *str)
  590. {
  591. size_t len = strlen(str);
  592. memcpy(*bufp, str, len);
  593. *bufp += len;
  594. return len;
  595. }
  596. /* Translate kernel rule respresentation to struct audit_rule.
  597. * Exists for backward compatibility with userspace. */
  598. static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
  599. {
  600. struct audit_rule *rule;
  601. int i;
  602. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  603. if (unlikely(!rule))
  604. return NULL;
  605. rule->flags = krule->flags | krule->listnr;
  606. rule->action = krule->action;
  607. rule->field_count = krule->field_count;
  608. for (i = 0; i < rule->field_count; i++) {
  609. rule->values[i] = krule->fields[i].val;
  610. rule->fields[i] = krule->fields[i].type;
  611. if (krule->vers_ops == 1) {
  612. if (krule->fields[i].op & AUDIT_NOT_EQUAL)
  613. rule->fields[i] |= AUDIT_NEGATE;
  614. } else {
  615. rule->fields[i] |= krule->fields[i].op;
  616. }
  617. }
  618. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
  619. return rule;
  620. }
  621. /* Translate kernel rule respresentation to struct audit_rule_data. */
  622. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  623. {
  624. struct audit_rule_data *data;
  625. void *bufp;
  626. int i;
  627. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  628. if (unlikely(!data))
  629. return NULL;
  630. memset(data, 0, sizeof(*data));
  631. data->flags = krule->flags | krule->listnr;
  632. data->action = krule->action;
  633. data->field_count = krule->field_count;
  634. bufp = data->buf;
  635. for (i = 0; i < data->field_count; i++) {
  636. struct audit_field *f = &krule->fields[i];
  637. data->fields[i] = f->type;
  638. data->fieldflags[i] = f->op;
  639. switch(f->type) {
  640. case AUDIT_SUBJ_USER:
  641. case AUDIT_SUBJ_ROLE:
  642. case AUDIT_SUBJ_TYPE:
  643. case AUDIT_SUBJ_SEN:
  644. case AUDIT_SUBJ_CLR:
  645. case AUDIT_OBJ_USER:
  646. case AUDIT_OBJ_ROLE:
  647. case AUDIT_OBJ_TYPE:
  648. case AUDIT_OBJ_LEV_LOW:
  649. case AUDIT_OBJ_LEV_HIGH:
  650. data->buflen += data->values[i] =
  651. audit_pack_string(&bufp, f->se_str);
  652. break;
  653. case AUDIT_WATCH:
  654. data->buflen += data->values[i] =
  655. audit_pack_string(&bufp, krule->watch->path);
  656. break;
  657. case AUDIT_FILTERKEY:
  658. data->buflen += data->values[i] =
  659. audit_pack_string(&bufp, krule->filterkey);
  660. break;
  661. default:
  662. data->values[i] = f->val;
  663. }
  664. }
  665. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  666. return data;
  667. }
  668. /* Compare two rules in kernel format. Considered success if rules
  669. * don't match. */
  670. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  671. {
  672. int i;
  673. if (a->flags != b->flags ||
  674. a->listnr != b->listnr ||
  675. a->action != b->action ||
  676. a->field_count != b->field_count)
  677. return 1;
  678. for (i = 0; i < a->field_count; i++) {
  679. if (a->fields[i].type != b->fields[i].type ||
  680. a->fields[i].op != b->fields[i].op)
  681. return 1;
  682. switch(a->fields[i].type) {
  683. case AUDIT_SUBJ_USER:
  684. case AUDIT_SUBJ_ROLE:
  685. case AUDIT_SUBJ_TYPE:
  686. case AUDIT_SUBJ_SEN:
  687. case AUDIT_SUBJ_CLR:
  688. case AUDIT_OBJ_USER:
  689. case AUDIT_OBJ_ROLE:
  690. case AUDIT_OBJ_TYPE:
  691. case AUDIT_OBJ_LEV_LOW:
  692. case AUDIT_OBJ_LEV_HIGH:
  693. if (strcmp(a->fields[i].se_str, b->fields[i].se_str))
  694. return 1;
  695. break;
  696. case AUDIT_WATCH:
  697. if (strcmp(a->watch->path, b->watch->path))
  698. return 1;
  699. break;
  700. case AUDIT_FILTERKEY:
  701. /* both filterkeys exist based on above type compare */
  702. if (strcmp(a->filterkey, b->filterkey))
  703. return 1;
  704. break;
  705. default:
  706. if (a->fields[i].val != b->fields[i].val)
  707. return 1;
  708. }
  709. }
  710. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  711. if (a->mask[i] != b->mask[i])
  712. return 1;
  713. return 0;
  714. }
  715. /* Duplicate the given audit watch. The new watch's rules list is initialized
  716. * to an empty list and wlist is undefined. */
  717. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  718. {
  719. char *path;
  720. struct audit_watch *new;
  721. path = kstrdup(old->path, GFP_KERNEL);
  722. if (unlikely(!path))
  723. return ERR_PTR(-ENOMEM);
  724. new = audit_init_watch(path);
  725. if (unlikely(IS_ERR(new))) {
  726. kfree(path);
  727. goto out;
  728. }
  729. new->dev = old->dev;
  730. new->ino = old->ino;
  731. get_inotify_watch(&old->parent->wdata);
  732. new->parent = old->parent;
  733. out:
  734. return new;
  735. }
  736. /* Duplicate selinux field information. The se_rule is opaque, so must be
  737. * re-initialized. */
  738. static inline int audit_dupe_selinux_field(struct audit_field *df,
  739. struct audit_field *sf)
  740. {
  741. int ret = 0;
  742. char *se_str;
  743. /* our own copy of se_str */
  744. se_str = kstrdup(sf->se_str, GFP_KERNEL);
  745. if (unlikely(!se_str))
  746. return -ENOMEM;
  747. df->se_str = se_str;
  748. /* our own (refreshed) copy of se_rule */
  749. ret = selinux_audit_rule_init(df->type, df->op, df->se_str,
  750. &df->se_rule);
  751. /* Keep currently invalid fields around in case they
  752. * become valid after a policy reload. */
  753. if (ret == -EINVAL) {
  754. printk(KERN_WARNING "audit rule for selinux \'%s\' is "
  755. "invalid\n", df->se_str);
  756. ret = 0;
  757. }
  758. return ret;
  759. }
  760. /* Duplicate an audit rule. This will be a deep copy with the exception
  761. * of the watch - that pointer is carried over. The selinux specific fields
  762. * will be updated in the copy. The point is to be able to replace the old
  763. * rule with the new rule in the filterlist, then free the old rule.
  764. * The rlist element is undefined; list manipulations are handled apart from
  765. * the initial copy. */
  766. static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
  767. struct audit_watch *watch)
  768. {
  769. u32 fcount = old->field_count;
  770. struct audit_entry *entry;
  771. struct audit_krule *new;
  772. char *fk;
  773. int i, err = 0;
  774. entry = audit_init_entry(fcount);
  775. if (unlikely(!entry))
  776. return ERR_PTR(-ENOMEM);
  777. new = &entry->rule;
  778. new->vers_ops = old->vers_ops;
  779. new->flags = old->flags;
  780. new->listnr = old->listnr;
  781. new->action = old->action;
  782. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  783. new->mask[i] = old->mask[i];
  784. new->buflen = old->buflen;
  785. new->inode_f = old->inode_f;
  786. new->watch = NULL;
  787. new->field_count = old->field_count;
  788. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  789. /* deep copy this information, updating the se_rule fields, because
  790. * the originals will all be freed when the old rule is freed. */
  791. for (i = 0; i < fcount; i++) {
  792. switch (new->fields[i].type) {
  793. case AUDIT_SUBJ_USER:
  794. case AUDIT_SUBJ_ROLE:
  795. case AUDIT_SUBJ_TYPE:
  796. case AUDIT_SUBJ_SEN:
  797. case AUDIT_SUBJ_CLR:
  798. case AUDIT_OBJ_USER:
  799. case AUDIT_OBJ_ROLE:
  800. case AUDIT_OBJ_TYPE:
  801. case AUDIT_OBJ_LEV_LOW:
  802. case AUDIT_OBJ_LEV_HIGH:
  803. err = audit_dupe_selinux_field(&new->fields[i],
  804. &old->fields[i]);
  805. break;
  806. case AUDIT_FILTERKEY:
  807. fk = kstrdup(old->filterkey, GFP_KERNEL);
  808. if (unlikely(!fk))
  809. err = -ENOMEM;
  810. else
  811. new->filterkey = fk;
  812. }
  813. if (err) {
  814. audit_free_rule(entry);
  815. return ERR_PTR(err);
  816. }
  817. }
  818. if (watch) {
  819. audit_get_watch(watch);
  820. new->watch = watch;
  821. }
  822. return entry;
  823. }
  824. /* Update inode info in audit rules based on filesystem event. */
  825. static void audit_update_watch(struct audit_parent *parent,
  826. const char *dname, dev_t dev,
  827. unsigned long ino, unsigned invalidating)
  828. {
  829. struct audit_watch *owatch, *nwatch, *nextw;
  830. struct audit_krule *r, *nextr;
  831. struct audit_entry *oentry, *nentry;
  832. struct audit_buffer *ab;
  833. mutex_lock(&audit_filter_mutex);
  834. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  835. if (audit_compare_dname_path(dname, owatch->path, NULL))
  836. continue;
  837. /* If the update involves invalidating rules, do the inode-based
  838. * filtering now, so we don't omit records. */
  839. if (invalidating && current->audit_context &&
  840. audit_filter_inodes(current, current->audit_context) == AUDIT_RECORD_CONTEXT)
  841. audit_set_auditable(current->audit_context);
  842. nwatch = audit_dupe_watch(owatch);
  843. if (unlikely(IS_ERR(nwatch))) {
  844. mutex_unlock(&audit_filter_mutex);
  845. audit_panic("error updating watch, skipping");
  846. return;
  847. }
  848. nwatch->dev = dev;
  849. nwatch->ino = ino;
  850. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  851. oentry = container_of(r, struct audit_entry, rule);
  852. list_del(&oentry->rule.rlist);
  853. list_del_rcu(&oentry->list);
  854. nentry = audit_dupe_rule(&oentry->rule, nwatch);
  855. if (unlikely(IS_ERR(nentry)))
  856. audit_panic("error updating watch, removing");
  857. else {
  858. int h = audit_hash_ino((u32)ino);
  859. list_add(&nentry->rule.rlist, &nwatch->rules);
  860. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  861. }
  862. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  863. }
  864. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  865. audit_log_format(ab, "op=updated rules specifying path=");
  866. audit_log_untrustedstring(ab, owatch->path);
  867. audit_log_format(ab, " with dev=%u ino=%lu\n", dev, ino);
  868. audit_log_format(ab, " list=%d res=1", r->listnr);
  869. audit_log_end(ab);
  870. audit_remove_watch(owatch);
  871. goto add_watch_to_parent; /* event applies to a single watch */
  872. }
  873. mutex_unlock(&audit_filter_mutex);
  874. return;
  875. add_watch_to_parent:
  876. list_add(&nwatch->wlist, &parent->watches);
  877. mutex_unlock(&audit_filter_mutex);
  878. return;
  879. }
  880. /* Remove all watches & rules associated with a parent that is going away. */
  881. static void audit_remove_parent_watches(struct audit_parent *parent)
  882. {
  883. struct audit_watch *w, *nextw;
  884. struct audit_krule *r, *nextr;
  885. struct audit_entry *e;
  886. struct audit_buffer *ab;
  887. mutex_lock(&audit_filter_mutex);
  888. parent->flags |= AUDIT_PARENT_INVALID;
  889. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  890. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  891. e = container_of(r, struct audit_entry, rule);
  892. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  893. audit_log_format(ab, "op=remove rule path=");
  894. audit_log_untrustedstring(ab, w->path);
  895. if (r->filterkey) {
  896. audit_log_format(ab, " key=");
  897. audit_log_untrustedstring(ab, r->filterkey);
  898. } else
  899. audit_log_format(ab, " key=(null)");
  900. audit_log_format(ab, " list=%d res=1", r->listnr);
  901. audit_log_end(ab);
  902. list_del(&r->rlist);
  903. list_del_rcu(&e->list);
  904. call_rcu(&e->rcu, audit_free_rule_rcu);
  905. }
  906. audit_remove_watch(w);
  907. }
  908. mutex_unlock(&audit_filter_mutex);
  909. }
  910. /* Unregister inotify watches for parents on in_list.
  911. * Generates an IN_IGNORED event. */
  912. static void audit_inotify_unregister(struct list_head *in_list)
  913. {
  914. struct audit_parent *p, *n;
  915. list_for_each_entry_safe(p, n, in_list, ilist) {
  916. list_del(&p->ilist);
  917. inotify_rm_watch(audit_ih, &p->wdata);
  918. /* the put matching the get in audit_do_del_rule() */
  919. put_inotify_watch(&p->wdata);
  920. }
  921. }
  922. /* Find an existing audit rule.
  923. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  924. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  925. struct list_head *list)
  926. {
  927. struct audit_entry *e, *found = NULL;
  928. int h;
  929. if (entry->rule.watch) {
  930. /* we don't know the inode number, so must walk entire hash */
  931. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  932. list = &audit_inode_hash[h];
  933. list_for_each_entry(e, list, list)
  934. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  935. found = e;
  936. goto out;
  937. }
  938. }
  939. goto out;
  940. }
  941. list_for_each_entry(e, list, list)
  942. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  943. found = e;
  944. goto out;
  945. }
  946. out:
  947. return found;
  948. }
  949. /* Get path information necessary for adding watches. */
  950. static int audit_get_nd(char *path, struct nameidata **ndp,
  951. struct nameidata **ndw)
  952. {
  953. struct nameidata *ndparent, *ndwatch;
  954. int err;
  955. ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
  956. if (unlikely(!ndparent))
  957. return -ENOMEM;
  958. ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
  959. if (unlikely(!ndwatch)) {
  960. kfree(ndparent);
  961. return -ENOMEM;
  962. }
  963. err = path_lookup(path, LOOKUP_PARENT, ndparent);
  964. if (err) {
  965. kfree(ndparent);
  966. kfree(ndwatch);
  967. return err;
  968. }
  969. err = path_lookup(path, 0, ndwatch);
  970. if (err) {
  971. kfree(ndwatch);
  972. ndwatch = NULL;
  973. }
  974. *ndp = ndparent;
  975. *ndw = ndwatch;
  976. return 0;
  977. }
  978. /* Release resources used for watch path information. */
  979. static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
  980. {
  981. if (ndp) {
  982. path_release(ndp);
  983. kfree(ndp);
  984. }
  985. if (ndw) {
  986. path_release(ndw);
  987. kfree(ndw);
  988. }
  989. }
  990. /* Associate the given rule with an existing parent inotify_watch.
  991. * Caller must hold audit_filter_mutex. */
  992. static void audit_add_to_parent(struct audit_krule *krule,
  993. struct audit_parent *parent)
  994. {
  995. struct audit_watch *w, *watch = krule->watch;
  996. int watch_found = 0;
  997. list_for_each_entry(w, &parent->watches, wlist) {
  998. if (strcmp(watch->path, w->path))
  999. continue;
  1000. watch_found = 1;
  1001. /* put krule's and initial refs to temporary watch */
  1002. audit_put_watch(watch);
  1003. audit_put_watch(watch);
  1004. audit_get_watch(w);
  1005. krule->watch = watch = w;
  1006. break;
  1007. }
  1008. if (!watch_found) {
  1009. get_inotify_watch(&parent->wdata);
  1010. watch->parent = parent;
  1011. list_add(&watch->wlist, &parent->watches);
  1012. }
  1013. list_add(&krule->rlist, &watch->rules);
  1014. }
  1015. /* Find a matching watch entry, or add this one.
  1016. * Caller must hold audit_filter_mutex. */
  1017. static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
  1018. struct nameidata *ndw)
  1019. {
  1020. struct audit_watch *watch = krule->watch;
  1021. struct inotify_watch *i_watch;
  1022. struct audit_parent *parent;
  1023. int ret = 0;
  1024. /* update watch filter fields */
  1025. if (ndw) {
  1026. watch->dev = ndw->dentry->d_inode->i_sb->s_dev;
  1027. watch->ino = ndw->dentry->d_inode->i_ino;
  1028. }
  1029. /* The audit_filter_mutex must not be held during inotify calls because
  1030. * we hold it during inotify event callback processing. If an existing
  1031. * inotify watch is found, inotify_find_watch() grabs a reference before
  1032. * returning.
  1033. */
  1034. mutex_unlock(&audit_filter_mutex);
  1035. if (inotify_find_watch(audit_ih, ndp->dentry->d_inode, &i_watch) < 0) {
  1036. parent = audit_init_parent(ndp);
  1037. if (IS_ERR(parent)) {
  1038. /* caller expects mutex locked */
  1039. mutex_lock(&audit_filter_mutex);
  1040. return PTR_ERR(parent);
  1041. }
  1042. } else
  1043. parent = container_of(i_watch, struct audit_parent, wdata);
  1044. mutex_lock(&audit_filter_mutex);
  1045. /* parent was moved before we took audit_filter_mutex */
  1046. if (parent->flags & AUDIT_PARENT_INVALID)
  1047. ret = -ENOENT;
  1048. else
  1049. audit_add_to_parent(krule, parent);
  1050. /* match get in audit_init_parent or inotify_find_watch */
  1051. put_inotify_watch(&parent->wdata);
  1052. return ret;
  1053. }
  1054. /* Add rule to given filterlist if not a duplicate. */
  1055. static inline int audit_add_rule(struct audit_entry *entry,
  1056. struct list_head *list)
  1057. {
  1058. struct audit_entry *e;
  1059. struct audit_field *inode_f = entry->rule.inode_f;
  1060. struct audit_watch *watch = entry->rule.watch;
  1061. struct nameidata *ndp = NULL, *ndw = NULL;
  1062. int h, err;
  1063. #ifdef CONFIG_AUDITSYSCALL
  1064. int dont_count = 0;
  1065. /* If either of these, don't count towards total */
  1066. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  1067. entry->rule.listnr == AUDIT_FILTER_TYPE)
  1068. dont_count = 1;
  1069. #endif
  1070. if (inode_f) {
  1071. h = audit_hash_ino(inode_f->val);
  1072. list = &audit_inode_hash[h];
  1073. }
  1074. mutex_lock(&audit_filter_mutex);
  1075. e = audit_find_rule(entry, list);
  1076. mutex_unlock(&audit_filter_mutex);
  1077. if (e) {
  1078. err = -EEXIST;
  1079. goto error;
  1080. }
  1081. /* Avoid calling path_lookup under audit_filter_mutex. */
  1082. if (watch) {
  1083. err = audit_get_nd(watch->path, &ndp, &ndw);
  1084. if (err)
  1085. goto error;
  1086. }
  1087. mutex_lock(&audit_filter_mutex);
  1088. if (watch) {
  1089. /* audit_filter_mutex is dropped and re-taken during this call */
  1090. err = audit_add_watch(&entry->rule, ndp, ndw);
  1091. if (err) {
  1092. mutex_unlock(&audit_filter_mutex);
  1093. goto error;
  1094. }
  1095. h = audit_hash_ino((u32)watch->ino);
  1096. list = &audit_inode_hash[h];
  1097. }
  1098. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  1099. list_add_rcu(&entry->list, list);
  1100. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  1101. } else {
  1102. list_add_tail_rcu(&entry->list, list);
  1103. }
  1104. #ifdef CONFIG_AUDITSYSCALL
  1105. if (!dont_count)
  1106. audit_n_rules++;
  1107. if (!audit_match_signal(entry))
  1108. audit_signals++;
  1109. #endif
  1110. mutex_unlock(&audit_filter_mutex);
  1111. audit_put_nd(ndp, ndw); /* NULL args OK */
  1112. return 0;
  1113. error:
  1114. audit_put_nd(ndp, ndw); /* NULL args OK */
  1115. if (watch)
  1116. audit_put_watch(watch); /* tmp watch, matches initial get */
  1117. return err;
  1118. }
  1119. /* Remove an existing rule from filterlist. */
  1120. static inline int audit_del_rule(struct audit_entry *entry,
  1121. struct list_head *list)
  1122. {
  1123. struct audit_entry *e;
  1124. struct audit_field *inode_f = entry->rule.inode_f;
  1125. struct audit_watch *watch, *tmp_watch = entry->rule.watch;
  1126. LIST_HEAD(inotify_list);
  1127. int h, ret = 0;
  1128. #ifdef CONFIG_AUDITSYSCALL
  1129. int dont_count = 0;
  1130. /* If either of these, don't count towards total */
  1131. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  1132. entry->rule.listnr == AUDIT_FILTER_TYPE)
  1133. dont_count = 1;
  1134. #endif
  1135. if (inode_f) {
  1136. h = audit_hash_ino(inode_f->val);
  1137. list = &audit_inode_hash[h];
  1138. }
  1139. mutex_lock(&audit_filter_mutex);
  1140. e = audit_find_rule(entry, list);
  1141. if (!e) {
  1142. mutex_unlock(&audit_filter_mutex);
  1143. ret = -ENOENT;
  1144. goto out;
  1145. }
  1146. watch = e->rule.watch;
  1147. if (watch) {
  1148. struct audit_parent *parent = watch->parent;
  1149. list_del(&e->rule.rlist);
  1150. if (list_empty(&watch->rules)) {
  1151. audit_remove_watch(watch);
  1152. if (list_empty(&parent->watches)) {
  1153. /* Put parent on the inotify un-registration
  1154. * list. Grab a reference before releasing
  1155. * audit_filter_mutex, to be released in
  1156. * audit_inotify_unregister(). */
  1157. list_add(&parent->ilist, &inotify_list);
  1158. get_inotify_watch(&parent->wdata);
  1159. }
  1160. }
  1161. }
  1162. list_del_rcu(&e->list);
  1163. call_rcu(&e->rcu, audit_free_rule_rcu);
  1164. #ifdef CONFIG_AUDITSYSCALL
  1165. if (!dont_count)
  1166. audit_n_rules--;
  1167. if (!audit_match_signal(entry))
  1168. audit_signals--;
  1169. #endif
  1170. mutex_unlock(&audit_filter_mutex);
  1171. if (!list_empty(&inotify_list))
  1172. audit_inotify_unregister(&inotify_list);
  1173. out:
  1174. if (tmp_watch)
  1175. audit_put_watch(tmp_watch); /* match initial get */
  1176. return ret;
  1177. }
  1178. /* List rules using struct audit_rule. Exists for backward
  1179. * compatibility with userspace. */
  1180. static void audit_list(int pid, int seq, struct sk_buff_head *q)
  1181. {
  1182. struct sk_buff *skb;
  1183. struct audit_entry *entry;
  1184. int i;
  1185. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  1186. * iterator to sync with list writers. */
  1187. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  1188. list_for_each_entry(entry, &audit_filter_list[i], list) {
  1189. struct audit_rule *rule;
  1190. rule = audit_krule_to_rule(&entry->rule);
  1191. if (unlikely(!rule))
  1192. break;
  1193. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  1194. rule, sizeof(*rule));
  1195. if (skb)
  1196. skb_queue_tail(q, skb);
  1197. kfree(rule);
  1198. }
  1199. }
  1200. for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
  1201. list_for_each_entry(entry, &audit_inode_hash[i], list) {
  1202. struct audit_rule *rule;
  1203. rule = audit_krule_to_rule(&entry->rule);
  1204. if (unlikely(!rule))
  1205. break;
  1206. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  1207. rule, sizeof(*rule));
  1208. if (skb)
  1209. skb_queue_tail(q, skb);
  1210. kfree(rule);
  1211. }
  1212. }
  1213. skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  1214. if (skb)
  1215. skb_queue_tail(q, skb);
  1216. }
  1217. /* List rules using struct audit_rule_data. */
  1218. static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
  1219. {
  1220. struct sk_buff *skb;
  1221. struct audit_entry *e;
  1222. int i;
  1223. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  1224. * iterator to sync with list writers. */
  1225. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  1226. list_for_each_entry(e, &audit_filter_list[i], list) {
  1227. struct audit_rule_data *data;
  1228. data = audit_krule_to_data(&e->rule);
  1229. if (unlikely(!data))
  1230. break;
  1231. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  1232. data, sizeof(*data) + data->buflen);
  1233. if (skb)
  1234. skb_queue_tail(q, skb);
  1235. kfree(data);
  1236. }
  1237. }
  1238. for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
  1239. list_for_each_entry(e, &audit_inode_hash[i], list) {
  1240. struct audit_rule_data *data;
  1241. data = audit_krule_to_data(&e->rule);
  1242. if (unlikely(!data))
  1243. break;
  1244. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  1245. data, sizeof(*data) + data->buflen);
  1246. if (skb)
  1247. skb_queue_tail(q, skb);
  1248. kfree(data);
  1249. }
  1250. }
  1251. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  1252. if (skb)
  1253. skb_queue_tail(q, skb);
  1254. }
  1255. /* Log rule additions and removals */
  1256. static void audit_log_rule_change(uid_t loginuid, u32 sid, char *action,
  1257. struct audit_krule *rule, int res)
  1258. {
  1259. struct audit_buffer *ab;
  1260. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  1261. if (!ab)
  1262. return;
  1263. audit_log_format(ab, "auid=%u", loginuid);
  1264. if (sid) {
  1265. char *ctx = NULL;
  1266. u32 len;
  1267. if (selinux_sid_to_string(sid, &ctx, &len))
  1268. audit_log_format(ab, " ssid=%u", sid);
  1269. else
  1270. audit_log_format(ab, " subj=%s", ctx);
  1271. kfree(ctx);
  1272. }
  1273. audit_log_format(ab, " op=%s rule key=", action);
  1274. if (rule->filterkey)
  1275. audit_log_untrustedstring(ab, rule->filterkey);
  1276. else
  1277. audit_log_format(ab, "(null)");
  1278. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  1279. audit_log_end(ab);
  1280. }
  1281. /**
  1282. * audit_receive_filter - apply all rules to the specified message type
  1283. * @type: audit message type
  1284. * @pid: target pid for netlink audit messages
  1285. * @uid: target uid for netlink audit messages
  1286. * @seq: netlink audit message sequence (serial) number
  1287. * @data: payload data
  1288. * @datasz: size of payload data
  1289. * @loginuid: loginuid of sender
  1290. * @sid: SE Linux Security ID of sender
  1291. */
  1292. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  1293. size_t datasz, uid_t loginuid, u32 sid)
  1294. {
  1295. struct task_struct *tsk;
  1296. struct audit_netlink_list *dest;
  1297. int err = 0;
  1298. struct audit_entry *entry;
  1299. switch (type) {
  1300. case AUDIT_LIST:
  1301. case AUDIT_LIST_RULES:
  1302. /* We can't just spew out the rules here because we might fill
  1303. * the available socket buffer space and deadlock waiting for
  1304. * auditctl to read from it... which isn't ever going to
  1305. * happen if we're actually running in the context of auditctl
  1306. * trying to _send_ the stuff */
  1307. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1308. if (!dest)
  1309. return -ENOMEM;
  1310. dest->pid = pid;
  1311. skb_queue_head_init(&dest->q);
  1312. mutex_lock(&audit_filter_mutex);
  1313. if (type == AUDIT_LIST)
  1314. audit_list(pid, seq, &dest->q);
  1315. else
  1316. audit_list_rules(pid, seq, &dest->q);
  1317. mutex_unlock(&audit_filter_mutex);
  1318. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1319. if (IS_ERR(tsk)) {
  1320. skb_queue_purge(&dest->q);
  1321. kfree(dest);
  1322. err = PTR_ERR(tsk);
  1323. }
  1324. break;
  1325. case AUDIT_ADD:
  1326. case AUDIT_ADD_RULE:
  1327. if (type == AUDIT_ADD)
  1328. entry = audit_rule_to_entry(data);
  1329. else
  1330. entry = audit_data_to_entry(data, datasz);
  1331. if (IS_ERR(entry))
  1332. return PTR_ERR(entry);
  1333. err = audit_add_rule(entry,
  1334. &audit_filter_list[entry->rule.listnr]);
  1335. audit_log_rule_change(loginuid, sid, "add", &entry->rule, !err);
  1336. if (err)
  1337. audit_free_rule(entry);
  1338. break;
  1339. case AUDIT_DEL:
  1340. case AUDIT_DEL_RULE:
  1341. if (type == AUDIT_DEL)
  1342. entry = audit_rule_to_entry(data);
  1343. else
  1344. entry = audit_data_to_entry(data, datasz);
  1345. if (IS_ERR(entry))
  1346. return PTR_ERR(entry);
  1347. err = audit_del_rule(entry,
  1348. &audit_filter_list[entry->rule.listnr]);
  1349. audit_log_rule_change(loginuid, sid, "remove", &entry->rule,
  1350. !err);
  1351. audit_free_rule(entry);
  1352. break;
  1353. default:
  1354. return -EINVAL;
  1355. }
  1356. return err;
  1357. }
  1358. int audit_comparator(const u32 left, const u32 op, const u32 right)
  1359. {
  1360. switch (op) {
  1361. case AUDIT_EQUAL:
  1362. return (left == right);
  1363. case AUDIT_NOT_EQUAL:
  1364. return (left != right);
  1365. case AUDIT_LESS_THAN:
  1366. return (left < right);
  1367. case AUDIT_LESS_THAN_OR_EQUAL:
  1368. return (left <= right);
  1369. case AUDIT_GREATER_THAN:
  1370. return (left > right);
  1371. case AUDIT_GREATER_THAN_OR_EQUAL:
  1372. return (left >= right);
  1373. case AUDIT_BIT_MASK:
  1374. return (left & right);
  1375. case AUDIT_BIT_TEST:
  1376. return ((left & right) == right);
  1377. }
  1378. BUG();
  1379. return 0;
  1380. }
  1381. /* Compare given dentry name with last component in given path,
  1382. * return of 0 indicates a match. */
  1383. int audit_compare_dname_path(const char *dname, const char *path,
  1384. int *dirlen)
  1385. {
  1386. int dlen, plen;
  1387. const char *p;
  1388. if (!dname || !path)
  1389. return 1;
  1390. dlen = strlen(dname);
  1391. plen = strlen(path);
  1392. if (plen < dlen)
  1393. return 1;
  1394. /* disregard trailing slashes */
  1395. p = path + plen - 1;
  1396. while ((*p == '/') && (p > path))
  1397. p--;
  1398. /* find last path component */
  1399. p = p - dlen + 1;
  1400. if (p < path)
  1401. return 1;
  1402. else if (p > path) {
  1403. if (*--p != '/')
  1404. return 1;
  1405. else
  1406. p++;
  1407. }
  1408. /* return length of path's directory component */
  1409. if (dirlen)
  1410. *dirlen = p - path;
  1411. return strncmp(p, dname, dlen);
  1412. }
  1413. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  1414. struct audit_krule *rule,
  1415. enum audit_state *state)
  1416. {
  1417. int i;
  1418. for (i = 0; i < rule->field_count; i++) {
  1419. struct audit_field *f = &rule->fields[i];
  1420. int result = 0;
  1421. switch (f->type) {
  1422. case AUDIT_PID:
  1423. result = audit_comparator(cb->creds.pid, f->op, f->val);
  1424. break;
  1425. case AUDIT_UID:
  1426. result = audit_comparator(cb->creds.uid, f->op, f->val);
  1427. break;
  1428. case AUDIT_GID:
  1429. result = audit_comparator(cb->creds.gid, f->op, f->val);
  1430. break;
  1431. case AUDIT_LOGINUID:
  1432. result = audit_comparator(cb->loginuid, f->op, f->val);
  1433. break;
  1434. }
  1435. if (!result)
  1436. return 0;
  1437. }
  1438. switch (rule->action) {
  1439. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  1440. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  1441. }
  1442. return 1;
  1443. }
  1444. int audit_filter_user(struct netlink_skb_parms *cb, int type)
  1445. {
  1446. enum audit_state state = AUDIT_DISABLED;
  1447. struct audit_entry *e;
  1448. int ret = 1;
  1449. rcu_read_lock();
  1450. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  1451. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  1452. if (state == AUDIT_DISABLED)
  1453. ret = 0;
  1454. break;
  1455. }
  1456. }
  1457. rcu_read_unlock();
  1458. return ret; /* Audit by default */
  1459. }
  1460. int audit_filter_type(int type)
  1461. {
  1462. struct audit_entry *e;
  1463. int result = 0;
  1464. rcu_read_lock();
  1465. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  1466. goto unlock_and_return;
  1467. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  1468. list) {
  1469. int i;
  1470. for (i = 0; i < e->rule.field_count; i++) {
  1471. struct audit_field *f = &e->rule.fields[i];
  1472. if (f->type == AUDIT_MSGTYPE) {
  1473. result = audit_comparator(type, f->op, f->val);
  1474. if (!result)
  1475. break;
  1476. }
  1477. }
  1478. if (result)
  1479. goto unlock_and_return;
  1480. }
  1481. unlock_and_return:
  1482. rcu_read_unlock();
  1483. return result;
  1484. }
  1485. /* Check to see if the rule contains any selinux fields. Returns 1 if there
  1486. are selinux fields specified in the rule, 0 otherwise. */
  1487. static inline int audit_rule_has_selinux(struct audit_krule *rule)
  1488. {
  1489. int i;
  1490. for (i = 0; i < rule->field_count; i++) {
  1491. struct audit_field *f = &rule->fields[i];
  1492. switch (f->type) {
  1493. case AUDIT_SUBJ_USER:
  1494. case AUDIT_SUBJ_ROLE:
  1495. case AUDIT_SUBJ_TYPE:
  1496. case AUDIT_SUBJ_SEN:
  1497. case AUDIT_SUBJ_CLR:
  1498. case AUDIT_OBJ_USER:
  1499. case AUDIT_OBJ_ROLE:
  1500. case AUDIT_OBJ_TYPE:
  1501. case AUDIT_OBJ_LEV_LOW:
  1502. case AUDIT_OBJ_LEV_HIGH:
  1503. return 1;
  1504. }
  1505. }
  1506. return 0;
  1507. }
  1508. /* This function will re-initialize the se_rule field of all applicable rules.
  1509. * It will traverse the filter lists serarching for rules that contain selinux
  1510. * specific filter fields. When such a rule is found, it is copied, the
  1511. * selinux field is re-initialized, and the old rule is replaced with the
  1512. * updated rule. */
  1513. int selinux_audit_rule_update(void)
  1514. {
  1515. struct audit_entry *entry, *n, *nentry;
  1516. struct audit_watch *watch;
  1517. int i, err = 0;
  1518. /* audit_filter_mutex synchronizes the writers */
  1519. mutex_lock(&audit_filter_mutex);
  1520. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1521. list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
  1522. if (!audit_rule_has_selinux(&entry->rule))
  1523. continue;
  1524. watch = entry->rule.watch;
  1525. nentry = audit_dupe_rule(&entry->rule, watch);
  1526. if (unlikely(IS_ERR(nentry))) {
  1527. /* save the first error encountered for the
  1528. * return value */
  1529. if (!err)
  1530. err = PTR_ERR(nentry);
  1531. audit_panic("error updating selinux filters");
  1532. if (watch)
  1533. list_del(&entry->rule.rlist);
  1534. list_del_rcu(&entry->list);
  1535. } else {
  1536. if (watch) {
  1537. list_add(&nentry->rule.rlist,
  1538. &watch->rules);
  1539. list_del(&entry->rule.rlist);
  1540. }
  1541. list_replace_rcu(&entry->list, &nentry->list);
  1542. }
  1543. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1544. }
  1545. }
  1546. mutex_unlock(&audit_filter_mutex);
  1547. return err;
  1548. }
  1549. /* Update watch data in audit rules based on inotify events. */
  1550. void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
  1551. u32 cookie, const char *dname, struct inode *inode)
  1552. {
  1553. struct audit_parent *parent;
  1554. parent = container_of(i_watch, struct audit_parent, wdata);
  1555. if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
  1556. audit_update_watch(parent, dname, inode->i_sb->s_dev,
  1557. inode->i_ino, 0);
  1558. else if (mask & (IN_DELETE|IN_MOVED_FROM))
  1559. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  1560. /* inotify automatically removes the watch and sends IN_IGNORED */
  1561. else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
  1562. audit_remove_parent_watches(parent);
  1563. /* inotify does not remove the watch, so remove it manually */
  1564. else if(mask & IN_MOVE_SELF) {
  1565. audit_remove_parent_watches(parent);
  1566. inotify_remove_watch_locked(audit_ih, i_watch);
  1567. } else if (mask & IN_IGNORED)
  1568. put_inotify_watch(i_watch);
  1569. }