auditfilter.c 33 KB

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