auditfilter.c 33 KB

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