auditfilter.c 33 KB

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