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