auditfilter.c 33 KB

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