auditfilter.c 34 KB

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