auditfilter.c 34 KB

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