auditfilter.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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/security.h>
  30. #include "audit.h"
  31. /*
  32. * Locking model:
  33. *
  34. * audit_filter_mutex:
  35. * Synchronizes writes and blocking reads of audit's filterlist
  36. * data. Rcu is used to traverse the filterlist and access
  37. * contents of structs audit_entry, audit_watch and opaque
  38. * LSM rules during filtering. If modified, these structures
  39. * must be copied and replace their counterparts in the filterlist.
  40. * An audit_parent struct is not accessed during filtering, so may
  41. * be written directly provided audit_filter_mutex is held.
  42. */
  43. /* Audit filter lists, defined in <linux/audit.h> */
  44. struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  45. LIST_HEAD_INIT(audit_filter_list[0]),
  46. LIST_HEAD_INIT(audit_filter_list[1]),
  47. LIST_HEAD_INIT(audit_filter_list[2]),
  48. LIST_HEAD_INIT(audit_filter_list[3]),
  49. LIST_HEAD_INIT(audit_filter_list[4]),
  50. LIST_HEAD_INIT(audit_filter_list[5]),
  51. #if AUDIT_NR_FILTERS != 6
  52. #error Fix audit_filter_list initialiser
  53. #endif
  54. };
  55. static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
  56. LIST_HEAD_INIT(audit_rules_list[0]),
  57. LIST_HEAD_INIT(audit_rules_list[1]),
  58. LIST_HEAD_INIT(audit_rules_list[2]),
  59. LIST_HEAD_INIT(audit_rules_list[3]),
  60. LIST_HEAD_INIT(audit_rules_list[4]),
  61. LIST_HEAD_INIT(audit_rules_list[5]),
  62. };
  63. DEFINE_MUTEX(audit_filter_mutex);
  64. static inline void audit_free_rule(struct audit_entry *e)
  65. {
  66. int i;
  67. struct audit_krule *erule = &e->rule;
  68. /* some rules don't have associated watches */
  69. if (erule->watch)
  70. audit_put_watch(erule->watch);
  71. if (erule->fields)
  72. for (i = 0; i < erule->field_count; i++) {
  73. struct audit_field *f = &erule->fields[i];
  74. kfree(f->lsm_str);
  75. security_audit_rule_free(f->lsm_rule);
  76. }
  77. kfree(erule->fields);
  78. kfree(erule->filterkey);
  79. kfree(e);
  80. }
  81. void audit_free_rule_rcu(struct rcu_head *head)
  82. {
  83. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  84. audit_free_rule(e);
  85. }
  86. /* Initialize an audit filterlist entry. */
  87. static inline struct audit_entry *audit_init_entry(u32 field_count)
  88. {
  89. struct audit_entry *entry;
  90. struct audit_field *fields;
  91. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  92. if (unlikely(!entry))
  93. return NULL;
  94. fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
  95. if (unlikely(!fields)) {
  96. kfree(entry);
  97. return NULL;
  98. }
  99. entry->rule.fields = fields;
  100. return entry;
  101. }
  102. /* Unpack a filter field's string representation from user-space
  103. * buffer. */
  104. char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
  105. {
  106. char *str;
  107. if (!*bufp || (len == 0) || (len > *remain))
  108. return ERR_PTR(-EINVAL);
  109. /* Of the currently implemented string fields, PATH_MAX
  110. * defines the longest valid length.
  111. */
  112. if (len > PATH_MAX)
  113. return ERR_PTR(-ENAMETOOLONG);
  114. str = kmalloc(len + 1, GFP_KERNEL);
  115. if (unlikely(!str))
  116. return ERR_PTR(-ENOMEM);
  117. memcpy(str, *bufp, len);
  118. str[len] = 0;
  119. *bufp += len;
  120. *remain -= len;
  121. return str;
  122. }
  123. /* Translate an inode field to kernel respresentation. */
  124. static inline int audit_to_inode(struct audit_krule *krule,
  125. struct audit_field *f)
  126. {
  127. if (krule->listnr != AUDIT_FILTER_EXIT ||
  128. krule->watch || krule->inode_f || krule->tree ||
  129. (f->op != Audit_equal && f->op != Audit_not_equal))
  130. return -EINVAL;
  131. krule->inode_f = f;
  132. return 0;
  133. }
  134. static __u32 *classes[AUDIT_SYSCALL_CLASSES];
  135. int __init audit_register_class(int class, unsigned *list)
  136. {
  137. __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
  138. if (!p)
  139. return -ENOMEM;
  140. while (*list != ~0U) {
  141. unsigned n = *list++;
  142. if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
  143. kfree(p);
  144. return -EINVAL;
  145. }
  146. p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
  147. }
  148. if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
  149. kfree(p);
  150. return -EINVAL;
  151. }
  152. classes[class] = p;
  153. return 0;
  154. }
  155. int audit_match_class(int class, unsigned syscall)
  156. {
  157. if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
  158. return 0;
  159. if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
  160. return 0;
  161. return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
  162. }
  163. #ifdef CONFIG_AUDITSYSCALL
  164. static inline int audit_match_class_bits(int class, u32 *mask)
  165. {
  166. int i;
  167. if (classes[class]) {
  168. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  169. if (mask[i] & classes[class][i])
  170. return 0;
  171. }
  172. return 1;
  173. }
  174. static int audit_match_signal(struct audit_entry *entry)
  175. {
  176. struct audit_field *arch = entry->rule.arch_f;
  177. if (!arch) {
  178. /* When arch is unspecified, we must check both masks on biarch
  179. * as syscall number alone is ambiguous. */
  180. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  181. entry->rule.mask) &&
  182. audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  183. entry->rule.mask));
  184. }
  185. switch(audit_classify_arch(arch->val)) {
  186. case 0: /* native */
  187. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  188. entry->rule.mask));
  189. case 1: /* 32bit on biarch */
  190. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  191. entry->rule.mask));
  192. default:
  193. return 1;
  194. }
  195. }
  196. #endif
  197. /* Common user-space to kernel rule translation. */
  198. static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
  199. {
  200. unsigned listnr;
  201. struct audit_entry *entry;
  202. int i, err;
  203. err = -EINVAL;
  204. listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
  205. switch(listnr) {
  206. default:
  207. goto exit_err;
  208. case AUDIT_FILTER_USER:
  209. case AUDIT_FILTER_TYPE:
  210. #ifdef CONFIG_AUDITSYSCALL
  211. case AUDIT_FILTER_ENTRY:
  212. case AUDIT_FILTER_EXIT:
  213. case AUDIT_FILTER_TASK:
  214. #endif
  215. ;
  216. }
  217. if (unlikely(rule->action == AUDIT_POSSIBLE)) {
  218. printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
  219. goto exit_err;
  220. }
  221. if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
  222. goto exit_err;
  223. if (rule->field_count > AUDIT_MAX_FIELDS)
  224. goto exit_err;
  225. err = -ENOMEM;
  226. entry = audit_init_entry(rule->field_count);
  227. if (!entry)
  228. goto exit_err;
  229. entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
  230. entry->rule.listnr = listnr;
  231. entry->rule.action = rule->action;
  232. entry->rule.field_count = rule->field_count;
  233. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  234. entry->rule.mask[i] = rule->mask[i];
  235. for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
  236. int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
  237. __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
  238. __u32 *class;
  239. if (!(*p & AUDIT_BIT(bit)))
  240. continue;
  241. *p &= ~AUDIT_BIT(bit);
  242. class = classes[i];
  243. if (class) {
  244. int j;
  245. for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
  246. entry->rule.mask[j] |= class[j];
  247. }
  248. }
  249. return entry;
  250. exit_err:
  251. return ERR_PTR(err);
  252. }
  253. static u32 audit_ops[] =
  254. {
  255. [Audit_equal] = AUDIT_EQUAL,
  256. [Audit_not_equal] = AUDIT_NOT_EQUAL,
  257. [Audit_bitmask] = AUDIT_BIT_MASK,
  258. [Audit_bittest] = AUDIT_BIT_TEST,
  259. [Audit_lt] = AUDIT_LESS_THAN,
  260. [Audit_gt] = AUDIT_GREATER_THAN,
  261. [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL,
  262. [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL,
  263. };
  264. static u32 audit_to_op(u32 op)
  265. {
  266. u32 n;
  267. for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
  268. ;
  269. return n;
  270. }
  271. /* Translate struct audit_rule to kernel's rule respresentation.
  272. * Exists for backward compatibility with userspace. */
  273. static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
  274. {
  275. struct audit_entry *entry;
  276. int err = 0;
  277. int i;
  278. entry = audit_to_entry_common(rule);
  279. if (IS_ERR(entry))
  280. goto exit_nofree;
  281. for (i = 0; i < rule->field_count; i++) {
  282. struct audit_field *f = &entry->rule.fields[i];
  283. u32 n;
  284. n = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
  285. /* Support for legacy operators where
  286. * AUDIT_NEGATE bit signifies != and otherwise assumes == */
  287. if (n & AUDIT_NEGATE)
  288. f->op = Audit_not_equal;
  289. else if (!n)
  290. f->op = Audit_equal;
  291. else
  292. f->op = audit_to_op(n);
  293. entry->rule.vers_ops = (n & AUDIT_OPERATORS) ? 2 : 1;
  294. f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
  295. f->val = rule->values[i];
  296. err = -EINVAL;
  297. if (f->op == Audit_bad)
  298. goto exit_free;
  299. switch(f->type) {
  300. default:
  301. goto exit_free;
  302. case AUDIT_PID:
  303. case AUDIT_UID:
  304. case AUDIT_EUID:
  305. case AUDIT_SUID:
  306. case AUDIT_FSUID:
  307. case AUDIT_GID:
  308. case AUDIT_EGID:
  309. case AUDIT_SGID:
  310. case AUDIT_FSGID:
  311. case AUDIT_LOGINUID:
  312. case AUDIT_PERS:
  313. case AUDIT_MSGTYPE:
  314. case AUDIT_PPID:
  315. case AUDIT_DEVMAJOR:
  316. case AUDIT_DEVMINOR:
  317. case AUDIT_EXIT:
  318. case AUDIT_SUCCESS:
  319. /* bit ops are only useful on syscall args */
  320. if (f->op == Audit_bitmask || f->op == Audit_bittest)
  321. goto exit_free;
  322. break;
  323. case AUDIT_ARG0:
  324. case AUDIT_ARG1:
  325. case AUDIT_ARG2:
  326. case AUDIT_ARG3:
  327. break;
  328. /* arch is only allowed to be = or != */
  329. case AUDIT_ARCH:
  330. if (f->op != Audit_not_equal && f->op != Audit_equal)
  331. goto exit_free;
  332. entry->rule.arch_f = f;
  333. break;
  334. case AUDIT_PERM:
  335. if (f->val & ~15)
  336. goto exit_free;
  337. break;
  338. case AUDIT_FILETYPE:
  339. if ((f->val & ~S_IFMT) > S_IFMT)
  340. goto exit_free;
  341. break;
  342. case AUDIT_INODE:
  343. err = audit_to_inode(&entry->rule, f);
  344. if (err)
  345. goto exit_free;
  346. break;
  347. }
  348. }
  349. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  350. entry->rule.inode_f = NULL;
  351. exit_nofree:
  352. return entry;
  353. exit_free:
  354. audit_free_rule(entry);
  355. return ERR_PTR(err);
  356. }
  357. /* Translate struct audit_rule_data to kernel's rule respresentation. */
  358. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  359. size_t datasz)
  360. {
  361. int err = 0;
  362. struct audit_entry *entry;
  363. void *bufp;
  364. size_t remain = datasz - sizeof(struct audit_rule_data);
  365. int i;
  366. char *str;
  367. entry = audit_to_entry_common((struct audit_rule *)data);
  368. if (IS_ERR(entry))
  369. goto exit_nofree;
  370. bufp = data->buf;
  371. entry->rule.vers_ops = 2;
  372. for (i = 0; i < data->field_count; i++) {
  373. struct audit_field *f = &entry->rule.fields[i];
  374. err = -EINVAL;
  375. f->op = audit_to_op(data->fieldflags[i]);
  376. if (f->op == Audit_bad)
  377. goto exit_free;
  378. f->type = data->fields[i];
  379. f->val = data->values[i];
  380. f->lsm_str = NULL;
  381. f->lsm_rule = NULL;
  382. switch(f->type) {
  383. case AUDIT_PID:
  384. case AUDIT_UID:
  385. case AUDIT_EUID:
  386. case AUDIT_SUID:
  387. case AUDIT_FSUID:
  388. case AUDIT_GID:
  389. case AUDIT_EGID:
  390. case AUDIT_SGID:
  391. case AUDIT_FSGID:
  392. case AUDIT_LOGINUID:
  393. case AUDIT_PERS:
  394. case AUDIT_MSGTYPE:
  395. case AUDIT_PPID:
  396. case AUDIT_DEVMAJOR:
  397. case AUDIT_DEVMINOR:
  398. case AUDIT_EXIT:
  399. case AUDIT_SUCCESS:
  400. case AUDIT_ARG0:
  401. case AUDIT_ARG1:
  402. case AUDIT_ARG2:
  403. case AUDIT_ARG3:
  404. break;
  405. case AUDIT_ARCH:
  406. entry->rule.arch_f = f;
  407. break;
  408. case AUDIT_SUBJ_USER:
  409. case AUDIT_SUBJ_ROLE:
  410. case AUDIT_SUBJ_TYPE:
  411. case AUDIT_SUBJ_SEN:
  412. case AUDIT_SUBJ_CLR:
  413. case AUDIT_OBJ_USER:
  414. case AUDIT_OBJ_ROLE:
  415. case AUDIT_OBJ_TYPE:
  416. case AUDIT_OBJ_LEV_LOW:
  417. case AUDIT_OBJ_LEV_HIGH:
  418. str = audit_unpack_string(&bufp, &remain, f->val);
  419. if (IS_ERR(str))
  420. goto exit_free;
  421. entry->rule.buflen += f->val;
  422. err = security_audit_rule_init(f->type, f->op, str,
  423. (void **)&f->lsm_rule);
  424. /* Keep currently invalid fields around in case they
  425. * become valid after a policy reload. */
  426. if (err == -EINVAL) {
  427. printk(KERN_WARNING "audit rule for LSM "
  428. "\'%s\' is invalid\n", str);
  429. err = 0;
  430. }
  431. if (err) {
  432. kfree(str);
  433. goto exit_free;
  434. } else
  435. f->lsm_str = str;
  436. break;
  437. case AUDIT_WATCH:
  438. str = audit_unpack_string(&bufp, &remain, f->val);
  439. if (IS_ERR(str))
  440. goto exit_free;
  441. entry->rule.buflen += f->val;
  442. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  443. if (err) {
  444. kfree(str);
  445. goto exit_free;
  446. }
  447. break;
  448. case AUDIT_DIR:
  449. str = audit_unpack_string(&bufp, &remain, f->val);
  450. if (IS_ERR(str))
  451. goto exit_free;
  452. entry->rule.buflen += f->val;
  453. err = audit_make_tree(&entry->rule, str, f->op);
  454. kfree(str);
  455. if (err)
  456. goto exit_free;
  457. break;
  458. case AUDIT_INODE:
  459. err = audit_to_inode(&entry->rule, f);
  460. if (err)
  461. goto exit_free;
  462. break;
  463. case AUDIT_FILTERKEY:
  464. err = -EINVAL;
  465. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  466. goto exit_free;
  467. str = audit_unpack_string(&bufp, &remain, f->val);
  468. if (IS_ERR(str))
  469. goto exit_free;
  470. entry->rule.buflen += f->val;
  471. entry->rule.filterkey = str;
  472. break;
  473. case AUDIT_PERM:
  474. if (f->val & ~15)
  475. goto exit_free;
  476. break;
  477. case AUDIT_FILETYPE:
  478. if ((f->val & ~S_IFMT) > S_IFMT)
  479. goto exit_free;
  480. break;
  481. default:
  482. goto exit_free;
  483. }
  484. }
  485. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  486. entry->rule.inode_f = NULL;
  487. exit_nofree:
  488. return entry;
  489. exit_free:
  490. audit_free_rule(entry);
  491. return ERR_PTR(err);
  492. }
  493. /* Pack a filter field's string representation into data block. */
  494. static inline size_t audit_pack_string(void **bufp, const char *str)
  495. {
  496. size_t len = strlen(str);
  497. memcpy(*bufp, str, len);
  498. *bufp += len;
  499. return len;
  500. }
  501. /* Translate kernel rule respresentation to struct audit_rule.
  502. * Exists for backward compatibility with userspace. */
  503. static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
  504. {
  505. struct audit_rule *rule;
  506. int i;
  507. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  508. if (unlikely(!rule))
  509. return NULL;
  510. rule->flags = krule->flags | krule->listnr;
  511. rule->action = krule->action;
  512. rule->field_count = krule->field_count;
  513. for (i = 0; i < rule->field_count; i++) {
  514. rule->values[i] = krule->fields[i].val;
  515. rule->fields[i] = krule->fields[i].type;
  516. if (krule->vers_ops == 1) {
  517. if (krule->fields[i].op == Audit_not_equal)
  518. rule->fields[i] |= AUDIT_NEGATE;
  519. } else {
  520. rule->fields[i] |= audit_ops[krule->fields[i].op];
  521. }
  522. }
  523. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
  524. return rule;
  525. }
  526. /* Translate kernel rule respresentation to struct audit_rule_data. */
  527. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  528. {
  529. struct audit_rule_data *data;
  530. void *bufp;
  531. int i;
  532. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  533. if (unlikely(!data))
  534. return NULL;
  535. memset(data, 0, sizeof(*data));
  536. data->flags = krule->flags | krule->listnr;
  537. data->action = krule->action;
  538. data->field_count = krule->field_count;
  539. bufp = data->buf;
  540. for (i = 0; i < data->field_count; i++) {
  541. struct audit_field *f = &krule->fields[i];
  542. data->fields[i] = f->type;
  543. data->fieldflags[i] = audit_ops[f->op];
  544. switch(f->type) {
  545. case AUDIT_SUBJ_USER:
  546. case AUDIT_SUBJ_ROLE:
  547. case AUDIT_SUBJ_TYPE:
  548. case AUDIT_SUBJ_SEN:
  549. case AUDIT_SUBJ_CLR:
  550. case AUDIT_OBJ_USER:
  551. case AUDIT_OBJ_ROLE:
  552. case AUDIT_OBJ_TYPE:
  553. case AUDIT_OBJ_LEV_LOW:
  554. case AUDIT_OBJ_LEV_HIGH:
  555. data->buflen += data->values[i] =
  556. audit_pack_string(&bufp, f->lsm_str);
  557. break;
  558. case AUDIT_WATCH:
  559. data->buflen += data->values[i] =
  560. audit_pack_string(&bufp,
  561. audit_watch_path(krule->watch));
  562. break;
  563. case AUDIT_DIR:
  564. data->buflen += data->values[i] =
  565. audit_pack_string(&bufp,
  566. audit_tree_path(krule->tree));
  567. break;
  568. case AUDIT_FILTERKEY:
  569. data->buflen += data->values[i] =
  570. audit_pack_string(&bufp, krule->filterkey);
  571. break;
  572. default:
  573. data->values[i] = f->val;
  574. }
  575. }
  576. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  577. return data;
  578. }
  579. /* Compare two rules in kernel format. Considered success if rules
  580. * don't match. */
  581. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  582. {
  583. int i;
  584. if (a->flags != b->flags ||
  585. a->listnr != b->listnr ||
  586. a->action != b->action ||
  587. a->field_count != b->field_count)
  588. return 1;
  589. for (i = 0; i < a->field_count; i++) {
  590. if (a->fields[i].type != b->fields[i].type ||
  591. a->fields[i].op != b->fields[i].op)
  592. return 1;
  593. switch(a->fields[i].type) {
  594. case AUDIT_SUBJ_USER:
  595. case AUDIT_SUBJ_ROLE:
  596. case AUDIT_SUBJ_TYPE:
  597. case AUDIT_SUBJ_SEN:
  598. case AUDIT_SUBJ_CLR:
  599. case AUDIT_OBJ_USER:
  600. case AUDIT_OBJ_ROLE:
  601. case AUDIT_OBJ_TYPE:
  602. case AUDIT_OBJ_LEV_LOW:
  603. case AUDIT_OBJ_LEV_HIGH:
  604. if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
  605. return 1;
  606. break;
  607. case AUDIT_WATCH:
  608. if (strcmp(audit_watch_path(a->watch),
  609. audit_watch_path(b->watch)))
  610. return 1;
  611. break;
  612. case AUDIT_DIR:
  613. if (strcmp(audit_tree_path(a->tree),
  614. audit_tree_path(b->tree)))
  615. return 1;
  616. break;
  617. case AUDIT_FILTERKEY:
  618. /* both filterkeys exist based on above type compare */
  619. if (strcmp(a->filterkey, b->filterkey))
  620. return 1;
  621. break;
  622. default:
  623. if (a->fields[i].val != b->fields[i].val)
  624. return 1;
  625. }
  626. }
  627. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  628. if (a->mask[i] != b->mask[i])
  629. return 1;
  630. return 0;
  631. }
  632. /* Duplicate LSM field information. The lsm_rule is opaque, so must be
  633. * re-initialized. */
  634. static inline int audit_dupe_lsm_field(struct audit_field *df,
  635. struct audit_field *sf)
  636. {
  637. int ret = 0;
  638. char *lsm_str;
  639. /* our own copy of lsm_str */
  640. lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
  641. if (unlikely(!lsm_str))
  642. return -ENOMEM;
  643. df->lsm_str = lsm_str;
  644. /* our own (refreshed) copy of lsm_rule */
  645. ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
  646. (void **)&df->lsm_rule);
  647. /* Keep currently invalid fields around in case they
  648. * become valid after a policy reload. */
  649. if (ret == -EINVAL) {
  650. printk(KERN_WARNING "audit rule for LSM \'%s\' is "
  651. "invalid\n", df->lsm_str);
  652. ret = 0;
  653. }
  654. return ret;
  655. }
  656. /* Duplicate an audit rule. This will be a deep copy with the exception
  657. * of the watch - that pointer is carried over. The LSM specific fields
  658. * will be updated in the copy. The point is to be able to replace the old
  659. * rule with the new rule in the filterlist, then free the old rule.
  660. * The rlist element is undefined; list manipulations are handled apart from
  661. * the initial copy. */
  662. struct audit_entry *audit_dupe_rule(struct audit_krule *old,
  663. struct audit_watch *watch)
  664. {
  665. u32 fcount = old->field_count;
  666. struct audit_entry *entry;
  667. struct audit_krule *new;
  668. char *fk;
  669. int i, err = 0;
  670. entry = audit_init_entry(fcount);
  671. if (unlikely(!entry))
  672. return ERR_PTR(-ENOMEM);
  673. new = &entry->rule;
  674. new->vers_ops = old->vers_ops;
  675. new->flags = old->flags;
  676. new->listnr = old->listnr;
  677. new->action = old->action;
  678. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  679. new->mask[i] = old->mask[i];
  680. new->prio = old->prio;
  681. new->buflen = old->buflen;
  682. new->inode_f = old->inode_f;
  683. new->watch = NULL;
  684. new->field_count = old->field_count;
  685. /*
  686. * note that we are OK with not refcounting here; audit_match_tree()
  687. * never dereferences tree and we can't get false positives there
  688. * since we'd have to have rule gone from the list *and* removed
  689. * before the chunks found by lookup had been allocated, i.e. before
  690. * the beginning of list scan.
  691. */
  692. new->tree = old->tree;
  693. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  694. /* deep copy this information, updating the lsm_rule fields, because
  695. * the originals will all be freed when the old rule is freed. */
  696. for (i = 0; i < fcount; i++) {
  697. switch (new->fields[i].type) {
  698. case AUDIT_SUBJ_USER:
  699. case AUDIT_SUBJ_ROLE:
  700. case AUDIT_SUBJ_TYPE:
  701. case AUDIT_SUBJ_SEN:
  702. case AUDIT_SUBJ_CLR:
  703. case AUDIT_OBJ_USER:
  704. case AUDIT_OBJ_ROLE:
  705. case AUDIT_OBJ_TYPE:
  706. case AUDIT_OBJ_LEV_LOW:
  707. case AUDIT_OBJ_LEV_HIGH:
  708. err = audit_dupe_lsm_field(&new->fields[i],
  709. &old->fields[i]);
  710. break;
  711. case AUDIT_FILTERKEY:
  712. fk = kstrdup(old->filterkey, GFP_KERNEL);
  713. if (unlikely(!fk))
  714. err = -ENOMEM;
  715. else
  716. new->filterkey = fk;
  717. }
  718. if (err) {
  719. audit_free_rule(entry);
  720. return ERR_PTR(err);
  721. }
  722. }
  723. if (watch) {
  724. audit_get_watch(watch);
  725. new->watch = watch;
  726. }
  727. return entry;
  728. }
  729. /* Find an existing audit rule.
  730. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  731. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  732. struct list_head **p)
  733. {
  734. struct audit_entry *e, *found = NULL;
  735. struct list_head *list;
  736. int h;
  737. if (entry->rule.inode_f) {
  738. h = audit_hash_ino(entry->rule.inode_f->val);
  739. *p = list = &audit_inode_hash[h];
  740. } else if (entry->rule.watch) {
  741. /* we don't know the inode number, so must walk entire hash */
  742. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  743. list = &audit_inode_hash[h];
  744. list_for_each_entry(e, list, list)
  745. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  746. found = e;
  747. goto out;
  748. }
  749. }
  750. goto out;
  751. } else {
  752. *p = list = &audit_filter_list[entry->rule.listnr];
  753. }
  754. list_for_each_entry(e, list, list)
  755. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  756. found = e;
  757. goto out;
  758. }
  759. out:
  760. return found;
  761. }
  762. static u64 prio_low = ~0ULL/2;
  763. static u64 prio_high = ~0ULL/2 - 1;
  764. /* Add rule to given filterlist if not a duplicate. */
  765. static inline int audit_add_rule(struct audit_entry *entry)
  766. {
  767. struct audit_entry *e;
  768. struct audit_watch *watch = entry->rule.watch;
  769. struct audit_tree *tree = entry->rule.tree;
  770. struct list_head *list;
  771. int h, err;
  772. #ifdef CONFIG_AUDITSYSCALL
  773. int dont_count = 0;
  774. /* If either of these, don't count towards total */
  775. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  776. entry->rule.listnr == AUDIT_FILTER_TYPE)
  777. dont_count = 1;
  778. #endif
  779. mutex_lock(&audit_filter_mutex);
  780. e = audit_find_rule(entry, &list);
  781. if (e) {
  782. mutex_unlock(&audit_filter_mutex);
  783. err = -EEXIST;
  784. /* normally audit_add_tree_rule() will free it on failure */
  785. if (tree)
  786. audit_put_tree(tree);
  787. goto error;
  788. }
  789. if (watch) {
  790. /* audit_filter_mutex is dropped and re-taken during this call */
  791. err = audit_add_watch(&entry->rule);
  792. if (err) {
  793. mutex_unlock(&audit_filter_mutex);
  794. goto error;
  795. }
  796. /* entry->rule.watch may have changed during audit_add_watch() */
  797. watch = entry->rule.watch;
  798. h = audit_hash_ino((u32)audit_watch_inode(watch));
  799. list = &audit_inode_hash[h];
  800. }
  801. if (tree) {
  802. err = audit_add_tree_rule(&entry->rule);
  803. if (err) {
  804. mutex_unlock(&audit_filter_mutex);
  805. goto error;
  806. }
  807. }
  808. entry->rule.prio = ~0ULL;
  809. if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
  810. if (entry->rule.flags & AUDIT_FILTER_PREPEND)
  811. entry->rule.prio = ++prio_high;
  812. else
  813. entry->rule.prio = --prio_low;
  814. }
  815. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  816. list_add(&entry->rule.list,
  817. &audit_rules_list[entry->rule.listnr]);
  818. list_add_rcu(&entry->list, list);
  819. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  820. } else {
  821. list_add_tail(&entry->rule.list,
  822. &audit_rules_list[entry->rule.listnr]);
  823. list_add_tail_rcu(&entry->list, list);
  824. }
  825. #ifdef CONFIG_AUDITSYSCALL
  826. if (!dont_count)
  827. audit_n_rules++;
  828. if (!audit_match_signal(entry))
  829. audit_signals++;
  830. #endif
  831. mutex_unlock(&audit_filter_mutex);
  832. return 0;
  833. error:
  834. if (watch)
  835. audit_put_watch(watch); /* tmp watch, matches initial get */
  836. return err;
  837. }
  838. /* Remove an existing rule from filterlist. */
  839. static inline int audit_del_rule(struct audit_entry *entry)
  840. {
  841. struct audit_entry *e;
  842. struct audit_watch *watch = entry->rule.watch;
  843. struct audit_tree *tree = entry->rule.tree;
  844. struct list_head *list;
  845. LIST_HEAD(inotify_list);
  846. int ret = 0;
  847. #ifdef CONFIG_AUDITSYSCALL
  848. int dont_count = 0;
  849. /* If either of these, don't count towards total */
  850. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  851. entry->rule.listnr == AUDIT_FILTER_TYPE)
  852. dont_count = 1;
  853. #endif
  854. mutex_lock(&audit_filter_mutex);
  855. e = audit_find_rule(entry, &list);
  856. if (!e) {
  857. mutex_unlock(&audit_filter_mutex);
  858. ret = -ENOENT;
  859. goto out;
  860. }
  861. if (e->rule.watch)
  862. audit_remove_watch_rule(&e->rule, &inotify_list);
  863. if (e->rule.tree)
  864. audit_remove_tree_rule(&e->rule);
  865. list_del_rcu(&e->list);
  866. list_del(&e->rule.list);
  867. call_rcu(&e->rcu, audit_free_rule_rcu);
  868. #ifdef CONFIG_AUDITSYSCALL
  869. if (!dont_count)
  870. audit_n_rules--;
  871. if (!audit_match_signal(entry))
  872. audit_signals--;
  873. #endif
  874. mutex_unlock(&audit_filter_mutex);
  875. if (!list_empty(&inotify_list))
  876. audit_inotify_unregister(&inotify_list);
  877. out:
  878. if (watch)
  879. audit_put_watch(watch); /* match initial get */
  880. if (tree)
  881. audit_put_tree(tree); /* that's the temporary one */
  882. return ret;
  883. }
  884. /* List rules using struct audit_rule. Exists for backward
  885. * compatibility with userspace. */
  886. static void audit_list(int pid, int seq, struct sk_buff_head *q)
  887. {
  888. struct sk_buff *skb;
  889. struct audit_krule *r;
  890. int i;
  891. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  892. * iterator to sync with list writers. */
  893. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  894. list_for_each_entry(r, &audit_rules_list[i], list) {
  895. struct audit_rule *rule;
  896. rule = audit_krule_to_rule(r);
  897. if (unlikely(!rule))
  898. break;
  899. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  900. rule, sizeof(*rule));
  901. if (skb)
  902. skb_queue_tail(q, skb);
  903. kfree(rule);
  904. }
  905. }
  906. skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  907. if (skb)
  908. skb_queue_tail(q, skb);
  909. }
  910. /* List rules using struct audit_rule_data. */
  911. static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
  912. {
  913. struct sk_buff *skb;
  914. struct audit_krule *r;
  915. int i;
  916. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  917. * iterator to sync with list writers. */
  918. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  919. list_for_each_entry(r, &audit_rules_list[i], list) {
  920. struct audit_rule_data *data;
  921. data = audit_krule_to_data(r);
  922. if (unlikely(!data))
  923. break;
  924. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  925. data, sizeof(*data) + data->buflen);
  926. if (skb)
  927. skb_queue_tail(q, skb);
  928. kfree(data);
  929. }
  930. }
  931. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  932. if (skb)
  933. skb_queue_tail(q, skb);
  934. }
  935. /* Log rule additions and removals */
  936. static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
  937. char *action, struct audit_krule *rule,
  938. int res)
  939. {
  940. struct audit_buffer *ab;
  941. if (!audit_enabled)
  942. return;
  943. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  944. if (!ab)
  945. return;
  946. audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
  947. if (sid) {
  948. char *ctx = NULL;
  949. u32 len;
  950. if (security_secid_to_secctx(sid, &ctx, &len))
  951. audit_log_format(ab, " ssid=%u", sid);
  952. else {
  953. audit_log_format(ab, " subj=%s", ctx);
  954. security_release_secctx(ctx, len);
  955. }
  956. }
  957. audit_log_format(ab, " op=");
  958. audit_log_string(ab, action);
  959. audit_log_key(ab, rule->filterkey);
  960. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  961. audit_log_end(ab);
  962. }
  963. /**
  964. * audit_receive_filter - apply all rules to the specified message type
  965. * @type: audit message type
  966. * @pid: target pid for netlink audit messages
  967. * @uid: target uid for netlink audit messages
  968. * @seq: netlink audit message sequence (serial) number
  969. * @data: payload data
  970. * @datasz: size of payload data
  971. * @loginuid: loginuid of sender
  972. * @sessionid: sessionid for netlink audit message
  973. * @sid: SE Linux Security ID of sender
  974. */
  975. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  976. size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
  977. {
  978. struct task_struct *tsk;
  979. struct audit_netlink_list *dest;
  980. int err = 0;
  981. struct audit_entry *entry;
  982. switch (type) {
  983. case AUDIT_LIST:
  984. case AUDIT_LIST_RULES:
  985. /* We can't just spew out the rules here because we might fill
  986. * the available socket buffer space and deadlock waiting for
  987. * auditctl to read from it... which isn't ever going to
  988. * happen if we're actually running in the context of auditctl
  989. * trying to _send_ the stuff */
  990. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  991. if (!dest)
  992. return -ENOMEM;
  993. dest->pid = pid;
  994. skb_queue_head_init(&dest->q);
  995. mutex_lock(&audit_filter_mutex);
  996. if (type == AUDIT_LIST)
  997. audit_list(pid, seq, &dest->q);
  998. else
  999. audit_list_rules(pid, seq, &dest->q);
  1000. mutex_unlock(&audit_filter_mutex);
  1001. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1002. if (IS_ERR(tsk)) {
  1003. skb_queue_purge(&dest->q);
  1004. kfree(dest);
  1005. err = PTR_ERR(tsk);
  1006. }
  1007. break;
  1008. case AUDIT_ADD:
  1009. case AUDIT_ADD_RULE:
  1010. if (type == AUDIT_ADD)
  1011. entry = audit_rule_to_entry(data);
  1012. else
  1013. entry = audit_data_to_entry(data, datasz);
  1014. if (IS_ERR(entry))
  1015. return PTR_ERR(entry);
  1016. err = audit_add_rule(entry);
  1017. audit_log_rule_change(loginuid, sessionid, sid, "add rule",
  1018. &entry->rule, !err);
  1019. if (err)
  1020. audit_free_rule(entry);
  1021. break;
  1022. case AUDIT_DEL:
  1023. case AUDIT_DEL_RULE:
  1024. if (type == AUDIT_DEL)
  1025. entry = audit_rule_to_entry(data);
  1026. else
  1027. entry = audit_data_to_entry(data, datasz);
  1028. if (IS_ERR(entry))
  1029. return PTR_ERR(entry);
  1030. err = audit_del_rule(entry);
  1031. audit_log_rule_change(loginuid, sessionid, sid, "remove rule",
  1032. &entry->rule, !err);
  1033. audit_free_rule(entry);
  1034. break;
  1035. default:
  1036. return -EINVAL;
  1037. }
  1038. return err;
  1039. }
  1040. int audit_comparator(u32 left, u32 op, u32 right)
  1041. {
  1042. switch (op) {
  1043. case Audit_equal:
  1044. return (left == right);
  1045. case Audit_not_equal:
  1046. return (left != right);
  1047. case Audit_lt:
  1048. return (left < right);
  1049. case Audit_le:
  1050. return (left <= right);
  1051. case Audit_gt:
  1052. return (left > right);
  1053. case Audit_ge:
  1054. return (left >= right);
  1055. case Audit_bitmask:
  1056. return (left & right);
  1057. case Audit_bittest:
  1058. return ((left & right) == right);
  1059. default:
  1060. BUG();
  1061. return 0;
  1062. }
  1063. }
  1064. /* Compare given dentry name with last component in given path,
  1065. * return of 0 indicates a match. */
  1066. int audit_compare_dname_path(const char *dname, const char *path,
  1067. int *dirlen)
  1068. {
  1069. int dlen, plen;
  1070. const char *p;
  1071. if (!dname || !path)
  1072. return 1;
  1073. dlen = strlen(dname);
  1074. plen = strlen(path);
  1075. if (plen < dlen)
  1076. return 1;
  1077. /* disregard trailing slashes */
  1078. p = path + plen - 1;
  1079. while ((*p == '/') && (p > path))
  1080. p--;
  1081. /* find last path component */
  1082. p = p - dlen + 1;
  1083. if (p < path)
  1084. return 1;
  1085. else if (p > path) {
  1086. if (*--p != '/')
  1087. return 1;
  1088. else
  1089. p++;
  1090. }
  1091. /* return length of path's directory component */
  1092. if (dirlen)
  1093. *dirlen = p - path;
  1094. return strncmp(p, dname, dlen);
  1095. }
  1096. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  1097. struct audit_krule *rule,
  1098. enum audit_state *state)
  1099. {
  1100. int i;
  1101. for (i = 0; i < rule->field_count; i++) {
  1102. struct audit_field *f = &rule->fields[i];
  1103. int result = 0;
  1104. switch (f->type) {
  1105. case AUDIT_PID:
  1106. result = audit_comparator(cb->creds.pid, f->op, f->val);
  1107. break;
  1108. case AUDIT_UID:
  1109. result = audit_comparator(cb->creds.uid, f->op, f->val);
  1110. break;
  1111. case AUDIT_GID:
  1112. result = audit_comparator(cb->creds.gid, f->op, f->val);
  1113. break;
  1114. case AUDIT_LOGINUID:
  1115. result = audit_comparator(cb->loginuid, f->op, f->val);
  1116. break;
  1117. }
  1118. if (!result)
  1119. return 0;
  1120. }
  1121. switch (rule->action) {
  1122. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  1123. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  1124. }
  1125. return 1;
  1126. }
  1127. int audit_filter_user(struct netlink_skb_parms *cb)
  1128. {
  1129. enum audit_state state = AUDIT_DISABLED;
  1130. struct audit_entry *e;
  1131. int ret = 1;
  1132. rcu_read_lock();
  1133. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  1134. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  1135. if (state == AUDIT_DISABLED)
  1136. ret = 0;
  1137. break;
  1138. }
  1139. }
  1140. rcu_read_unlock();
  1141. return ret; /* Audit by default */
  1142. }
  1143. int audit_filter_type(int type)
  1144. {
  1145. struct audit_entry *e;
  1146. int result = 0;
  1147. rcu_read_lock();
  1148. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  1149. goto unlock_and_return;
  1150. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  1151. list) {
  1152. int i;
  1153. for (i = 0; i < e->rule.field_count; i++) {
  1154. struct audit_field *f = &e->rule.fields[i];
  1155. if (f->type == AUDIT_MSGTYPE) {
  1156. result = audit_comparator(type, f->op, f->val);
  1157. if (!result)
  1158. break;
  1159. }
  1160. }
  1161. if (result)
  1162. goto unlock_and_return;
  1163. }
  1164. unlock_and_return:
  1165. rcu_read_unlock();
  1166. return result;
  1167. }
  1168. static int update_lsm_rule(struct audit_krule *r)
  1169. {
  1170. struct audit_entry *entry = container_of(r, struct audit_entry, rule);
  1171. struct audit_entry *nentry;
  1172. struct audit_watch *watch;
  1173. struct audit_tree *tree;
  1174. int err = 0;
  1175. if (!security_audit_rule_known(r))
  1176. return 0;
  1177. watch = r->watch;
  1178. tree = r->tree;
  1179. nentry = audit_dupe_rule(r, watch);
  1180. if (IS_ERR(nentry)) {
  1181. /* save the first error encountered for the
  1182. * return value */
  1183. err = PTR_ERR(nentry);
  1184. audit_panic("error updating LSM filters");
  1185. if (watch)
  1186. list_del(&r->rlist);
  1187. list_del_rcu(&entry->list);
  1188. list_del(&r->list);
  1189. } else {
  1190. if (watch) {
  1191. list_add(&nentry->rule.rlist, audit_watch_rules(watch));
  1192. list_del(&r->rlist);
  1193. } else if (tree)
  1194. list_replace_init(&r->rlist, &nentry->rule.rlist);
  1195. list_replace_rcu(&entry->list, &nentry->list);
  1196. list_replace(&r->list, &nentry->rule.list);
  1197. }
  1198. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1199. return err;
  1200. }
  1201. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1202. * It will traverse the filter lists serarching for rules that contain LSM
  1203. * specific filter fields. When such a rule is found, it is copied, the
  1204. * LSM field is re-initialized, and the old rule is replaced with the
  1205. * updated rule. */
  1206. int audit_update_lsm_rules(void)
  1207. {
  1208. struct audit_krule *r, *n;
  1209. int i, err = 0;
  1210. /* audit_filter_mutex synchronizes the writers */
  1211. mutex_lock(&audit_filter_mutex);
  1212. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1213. list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
  1214. int res = update_lsm_rule(r);
  1215. if (!err)
  1216. err = res;
  1217. }
  1218. }
  1219. mutex_unlock(&audit_filter_mutex);
  1220. return err;
  1221. }