auditfilter.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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/netlink.h>
  25. #include <linux/selinux.h>
  26. #include "audit.h"
  27. /* There are three lists of rules -- one to search at task creation
  28. * time, one to search at syscall entry time, and another to search at
  29. * syscall exit time. */
  30. struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  31. LIST_HEAD_INIT(audit_filter_list[0]),
  32. LIST_HEAD_INIT(audit_filter_list[1]),
  33. LIST_HEAD_INIT(audit_filter_list[2]),
  34. LIST_HEAD_INIT(audit_filter_list[3]),
  35. LIST_HEAD_INIT(audit_filter_list[4]),
  36. LIST_HEAD_INIT(audit_filter_list[5]),
  37. #if AUDIT_NR_FILTERS != 6
  38. #error Fix audit_filter_list initialiser
  39. #endif
  40. };
  41. static inline void audit_free_rule(struct audit_entry *e)
  42. {
  43. int i;
  44. if (e->rule.fields)
  45. for (i = 0; i < e->rule.field_count; i++) {
  46. struct audit_field *f = &e->rule.fields[i];
  47. kfree(f->se_str);
  48. selinux_audit_rule_free(f->se_rule);
  49. }
  50. kfree(e->rule.fields);
  51. kfree(e);
  52. }
  53. static inline void audit_free_rule_rcu(struct rcu_head *head)
  54. {
  55. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  56. audit_free_rule(e);
  57. }
  58. /* Initialize an audit filterlist entry. */
  59. static inline struct audit_entry *audit_init_entry(u32 field_count)
  60. {
  61. struct audit_entry *entry;
  62. struct audit_field *fields;
  63. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  64. if (unlikely(!entry))
  65. return NULL;
  66. fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
  67. if (unlikely(!fields)) {
  68. kfree(entry);
  69. return NULL;
  70. }
  71. entry->rule.fields = fields;
  72. return entry;
  73. }
  74. /* Unpack a filter field's string representation from user-space
  75. * buffer. */
  76. static char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
  77. {
  78. char *str;
  79. if (!*bufp || (len == 0) || (len > *remain))
  80. return ERR_PTR(-EINVAL);
  81. /* Of the currently implemented string fields, PATH_MAX
  82. * defines the longest valid length.
  83. */
  84. if (len > PATH_MAX)
  85. return ERR_PTR(-ENAMETOOLONG);
  86. str = kmalloc(len + 1, GFP_KERNEL);
  87. if (unlikely(!str))
  88. return ERR_PTR(-ENOMEM);
  89. memcpy(str, *bufp, len);
  90. str[len] = 0;
  91. *bufp += len;
  92. *remain -= len;
  93. return str;
  94. }
  95. /* Common user-space to kernel rule translation. */
  96. static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
  97. {
  98. unsigned listnr;
  99. struct audit_entry *entry;
  100. int i, err;
  101. err = -EINVAL;
  102. listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
  103. switch(listnr) {
  104. default:
  105. goto exit_err;
  106. case AUDIT_FILTER_USER:
  107. case AUDIT_FILTER_TYPE:
  108. #ifdef CONFIG_AUDITSYSCALL
  109. case AUDIT_FILTER_ENTRY:
  110. case AUDIT_FILTER_EXIT:
  111. case AUDIT_FILTER_TASK:
  112. #endif
  113. ;
  114. }
  115. if (rule->action != AUDIT_NEVER && rule->action != AUDIT_POSSIBLE &&
  116. rule->action != AUDIT_ALWAYS)
  117. goto exit_err;
  118. if (rule->field_count > AUDIT_MAX_FIELDS)
  119. goto exit_err;
  120. err = -ENOMEM;
  121. entry = audit_init_entry(rule->field_count);
  122. if (!entry)
  123. goto exit_err;
  124. entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
  125. entry->rule.listnr = listnr;
  126. entry->rule.action = rule->action;
  127. entry->rule.field_count = rule->field_count;
  128. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  129. entry->rule.mask[i] = rule->mask[i];
  130. return entry;
  131. exit_err:
  132. return ERR_PTR(err);
  133. }
  134. /* Translate struct audit_rule to kernel's rule respresentation.
  135. * Exists for backward compatibility with userspace. */
  136. static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
  137. {
  138. struct audit_entry *entry;
  139. int err = 0;
  140. int i;
  141. entry = audit_to_entry_common(rule);
  142. if (IS_ERR(entry))
  143. goto exit_nofree;
  144. for (i = 0; i < rule->field_count; i++) {
  145. struct audit_field *f = &entry->rule.fields[i];
  146. f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
  147. f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
  148. f->val = rule->values[i];
  149. if (f->type & AUDIT_UNUSED_BITS ||
  150. f->type == AUDIT_SE_USER ||
  151. f->type == AUDIT_SE_ROLE ||
  152. f->type == AUDIT_SE_TYPE ||
  153. f->type == AUDIT_SE_SEN ||
  154. f->type == AUDIT_SE_CLR) {
  155. err = -EINVAL;
  156. goto exit_free;
  157. }
  158. entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
  159. /* Support for legacy operators where
  160. * AUDIT_NEGATE bit signifies != and otherwise assumes == */
  161. if (f->op & AUDIT_NEGATE)
  162. f->op = AUDIT_NOT_EQUAL;
  163. else if (!f->op)
  164. f->op = AUDIT_EQUAL;
  165. else if (f->op == AUDIT_OPERATORS) {
  166. err = -EINVAL;
  167. goto exit_free;
  168. }
  169. }
  170. exit_nofree:
  171. return entry;
  172. exit_free:
  173. audit_free_rule(entry);
  174. return ERR_PTR(err);
  175. }
  176. /* Translate struct audit_rule_data to kernel's rule respresentation. */
  177. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  178. size_t datasz)
  179. {
  180. int err = 0;
  181. struct audit_entry *entry;
  182. void *bufp;
  183. size_t remain = datasz - sizeof(struct audit_rule_data);
  184. int i;
  185. char *str;
  186. entry = audit_to_entry_common((struct audit_rule *)data);
  187. if (IS_ERR(entry))
  188. goto exit_nofree;
  189. bufp = data->buf;
  190. entry->rule.vers_ops = 2;
  191. for (i = 0; i < data->field_count; i++) {
  192. struct audit_field *f = &entry->rule.fields[i];
  193. err = -EINVAL;
  194. if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
  195. data->fieldflags[i] & ~AUDIT_OPERATORS)
  196. goto exit_free;
  197. f->op = data->fieldflags[i] & AUDIT_OPERATORS;
  198. f->type = data->fields[i];
  199. f->val = data->values[i];
  200. f->se_str = NULL;
  201. f->se_rule = NULL;
  202. switch(f->type) {
  203. case AUDIT_SE_USER:
  204. case AUDIT_SE_ROLE:
  205. case AUDIT_SE_TYPE:
  206. case AUDIT_SE_SEN:
  207. case AUDIT_SE_CLR:
  208. str = audit_unpack_string(&bufp, &remain, f->val);
  209. if (IS_ERR(str))
  210. goto exit_free;
  211. entry->rule.buflen += f->val;
  212. err = selinux_audit_rule_init(f->type, f->op, str,
  213. &f->se_rule);
  214. /* Keep currently invalid fields around in case they
  215. * become valid after a policy reload. */
  216. if (err == -EINVAL) {
  217. printk(KERN_WARNING "audit rule for selinux "
  218. "\'%s\' is invalid\n", str);
  219. err = 0;
  220. }
  221. if (err) {
  222. kfree(str);
  223. goto exit_free;
  224. } else
  225. f->se_str = str;
  226. break;
  227. }
  228. }
  229. exit_nofree:
  230. return entry;
  231. exit_free:
  232. audit_free_rule(entry);
  233. return ERR_PTR(err);
  234. }
  235. /* Pack a filter field's string representation into data block. */
  236. static inline size_t audit_pack_string(void **bufp, char *str)
  237. {
  238. size_t len = strlen(str);
  239. memcpy(*bufp, str, len);
  240. *bufp += len;
  241. return len;
  242. }
  243. /* Translate kernel rule respresentation to struct audit_rule.
  244. * Exists for backward compatibility with userspace. */
  245. static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
  246. {
  247. struct audit_rule *rule;
  248. int i;
  249. rule = kmalloc(sizeof(*rule), GFP_KERNEL);
  250. if (unlikely(!rule))
  251. return ERR_PTR(-ENOMEM);
  252. memset(rule, 0, sizeof(*rule));
  253. rule->flags = krule->flags | krule->listnr;
  254. rule->action = krule->action;
  255. rule->field_count = krule->field_count;
  256. for (i = 0; i < rule->field_count; i++) {
  257. rule->values[i] = krule->fields[i].val;
  258. rule->fields[i] = krule->fields[i].type;
  259. if (krule->vers_ops == 1) {
  260. if (krule->fields[i].op & AUDIT_NOT_EQUAL)
  261. rule->fields[i] |= AUDIT_NEGATE;
  262. } else {
  263. rule->fields[i] |= krule->fields[i].op;
  264. }
  265. }
  266. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
  267. return rule;
  268. }
  269. /* Translate kernel rule respresentation to struct audit_rule_data. */
  270. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  271. {
  272. struct audit_rule_data *data;
  273. void *bufp;
  274. int i;
  275. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  276. if (unlikely(!data))
  277. return ERR_PTR(-ENOMEM);
  278. memset(data, 0, sizeof(*data));
  279. data->flags = krule->flags | krule->listnr;
  280. data->action = krule->action;
  281. data->field_count = krule->field_count;
  282. bufp = data->buf;
  283. for (i = 0; i < data->field_count; i++) {
  284. struct audit_field *f = &krule->fields[i];
  285. data->fields[i] = f->type;
  286. data->fieldflags[i] = f->op;
  287. switch(f->type) {
  288. case AUDIT_SE_USER:
  289. case AUDIT_SE_ROLE:
  290. case AUDIT_SE_TYPE:
  291. case AUDIT_SE_SEN:
  292. case AUDIT_SE_CLR:
  293. data->buflen += data->values[i] =
  294. audit_pack_string(&bufp, f->se_str);
  295. break;
  296. default:
  297. data->values[i] = f->val;
  298. }
  299. }
  300. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  301. return data;
  302. }
  303. /* Compare two rules in kernel format. Considered success if rules
  304. * don't match. */
  305. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  306. {
  307. int i;
  308. if (a->flags != b->flags ||
  309. a->listnr != b->listnr ||
  310. a->action != b->action ||
  311. a->field_count != b->field_count)
  312. return 1;
  313. for (i = 0; i < a->field_count; i++) {
  314. if (a->fields[i].type != b->fields[i].type ||
  315. a->fields[i].op != b->fields[i].op)
  316. return 1;
  317. switch(a->fields[i].type) {
  318. case AUDIT_SE_USER:
  319. case AUDIT_SE_ROLE:
  320. case AUDIT_SE_TYPE:
  321. case AUDIT_SE_SEN:
  322. case AUDIT_SE_CLR:
  323. if (strcmp(a->fields[i].se_str, b->fields[i].se_str))
  324. return 1;
  325. break;
  326. default:
  327. if (a->fields[i].val != b->fields[i].val)
  328. return 1;
  329. }
  330. }
  331. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  332. if (a->mask[i] != b->mask[i])
  333. return 1;
  334. return 0;
  335. }
  336. /* Duplicate selinux field information. The se_rule is opaque, so must be
  337. * re-initialized. */
  338. static inline int audit_dupe_selinux_field(struct audit_field *df,
  339. struct audit_field *sf)
  340. {
  341. int ret = 0;
  342. char *se_str;
  343. /* our own copy of se_str */
  344. se_str = kstrdup(sf->se_str, GFP_KERNEL);
  345. if (unlikely(IS_ERR(se_str)))
  346. return -ENOMEM;
  347. df->se_str = se_str;
  348. /* our own (refreshed) copy of se_rule */
  349. ret = selinux_audit_rule_init(df->type, df->op, df->se_str,
  350. &df->se_rule);
  351. /* Keep currently invalid fields around in case they
  352. * become valid after a policy reload. */
  353. if (ret == -EINVAL) {
  354. printk(KERN_WARNING "audit rule for selinux \'%s\' is "
  355. "invalid\n", df->se_str);
  356. ret = 0;
  357. }
  358. return ret;
  359. }
  360. /* Duplicate an audit rule. This will be a deep copy with the exception
  361. * of the watch - that pointer is carried over. The selinux specific fields
  362. * will be updated in the copy. The point is to be able to replace the old
  363. * rule with the new rule in the filterlist, then free the old rule. */
  364. static struct audit_entry *audit_dupe_rule(struct audit_krule *old)
  365. {
  366. u32 fcount = old->field_count;
  367. struct audit_entry *entry;
  368. struct audit_krule *new;
  369. int i, err = 0;
  370. entry = audit_init_entry(fcount);
  371. if (unlikely(!entry))
  372. return ERR_PTR(-ENOMEM);
  373. new = &entry->rule;
  374. new->vers_ops = old->vers_ops;
  375. new->flags = old->flags;
  376. new->listnr = old->listnr;
  377. new->action = old->action;
  378. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  379. new->mask[i] = old->mask[i];
  380. new->buflen = old->buflen;
  381. new->field_count = old->field_count;
  382. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  383. /* deep copy this information, updating the se_rule fields, because
  384. * the originals will all be freed when the old rule is freed. */
  385. for (i = 0; i < fcount; i++) {
  386. switch (new->fields[i].type) {
  387. case AUDIT_SE_USER:
  388. case AUDIT_SE_ROLE:
  389. case AUDIT_SE_TYPE:
  390. case AUDIT_SE_SEN:
  391. case AUDIT_SE_CLR:
  392. err = audit_dupe_selinux_field(&new->fields[i],
  393. &old->fields[i]);
  394. }
  395. if (err) {
  396. audit_free_rule(entry);
  397. return ERR_PTR(err);
  398. }
  399. }
  400. return entry;
  401. }
  402. /* Add rule to given filterlist if not a duplicate. Protected by
  403. * audit_netlink_mutex. */
  404. static inline int audit_add_rule(struct audit_entry *entry,
  405. struct list_head *list)
  406. {
  407. struct audit_entry *e;
  408. /* Do not use the _rcu iterator here, since this is the only
  409. * addition routine. */
  410. list_for_each_entry(e, list, list) {
  411. if (!audit_compare_rule(&entry->rule, &e->rule))
  412. return -EEXIST;
  413. }
  414. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  415. list_add_rcu(&entry->list, list);
  416. } else {
  417. list_add_tail_rcu(&entry->list, list);
  418. }
  419. return 0;
  420. }
  421. /* Remove an existing rule from filterlist. Protected by
  422. * audit_netlink_mutex. */
  423. static inline int audit_del_rule(struct audit_entry *entry,
  424. struct list_head *list)
  425. {
  426. struct audit_entry *e;
  427. /* Do not use the _rcu iterator here, since this is the only
  428. * deletion routine. */
  429. list_for_each_entry(e, list, list) {
  430. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  431. list_del_rcu(&e->list);
  432. call_rcu(&e->rcu, audit_free_rule_rcu);
  433. return 0;
  434. }
  435. }
  436. return -ENOENT; /* No matching rule */
  437. }
  438. /* List rules using struct audit_rule. Exists for backward
  439. * compatibility with userspace. */
  440. static int audit_list(void *_dest)
  441. {
  442. int pid, seq;
  443. int *dest = _dest;
  444. struct audit_entry *entry;
  445. int i;
  446. pid = dest[0];
  447. seq = dest[1];
  448. kfree(dest);
  449. mutex_lock(&audit_netlink_mutex);
  450. /* The *_rcu iterators not needed here because we are
  451. always called with audit_netlink_mutex held. */
  452. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  453. list_for_each_entry(entry, &audit_filter_list[i], list) {
  454. struct audit_rule *rule;
  455. rule = audit_krule_to_rule(&entry->rule);
  456. if (unlikely(!rule))
  457. break;
  458. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  459. rule, sizeof(*rule));
  460. kfree(rule);
  461. }
  462. }
  463. audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  464. mutex_unlock(&audit_netlink_mutex);
  465. return 0;
  466. }
  467. /* List rules using struct audit_rule_data. */
  468. static int audit_list_rules(void *_dest)
  469. {
  470. int pid, seq;
  471. int *dest = _dest;
  472. struct audit_entry *e;
  473. int i;
  474. pid = dest[0];
  475. seq = dest[1];
  476. kfree(dest);
  477. mutex_lock(&audit_netlink_mutex);
  478. /* The *_rcu iterators not needed here because we are
  479. always called with audit_netlink_mutex held. */
  480. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  481. list_for_each_entry(e, &audit_filter_list[i], list) {
  482. struct audit_rule_data *data;
  483. data = audit_krule_to_data(&e->rule);
  484. if (unlikely(!data))
  485. break;
  486. audit_send_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  487. data, sizeof(*data));
  488. kfree(data);
  489. }
  490. }
  491. audit_send_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  492. mutex_unlock(&audit_netlink_mutex);
  493. return 0;
  494. }
  495. /**
  496. * audit_receive_filter - apply all rules to the specified message type
  497. * @type: audit message type
  498. * @pid: target pid for netlink audit messages
  499. * @uid: target uid for netlink audit messages
  500. * @seq: netlink audit message sequence (serial) number
  501. * @data: payload data
  502. * @datasz: size of payload data
  503. * @loginuid: loginuid of sender
  504. * @sid: SE Linux Security ID of sender
  505. */
  506. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  507. size_t datasz, uid_t loginuid, u32 sid)
  508. {
  509. struct task_struct *tsk;
  510. int *dest;
  511. int err = 0;
  512. struct audit_entry *entry;
  513. switch (type) {
  514. case AUDIT_LIST:
  515. case AUDIT_LIST_RULES:
  516. /* We can't just spew out the rules here because we might fill
  517. * the available socket buffer space and deadlock waiting for
  518. * auditctl to read from it... which isn't ever going to
  519. * happen if we're actually running in the context of auditctl
  520. * trying to _send_ the stuff */
  521. dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
  522. if (!dest)
  523. return -ENOMEM;
  524. dest[0] = pid;
  525. dest[1] = seq;
  526. if (type == AUDIT_LIST)
  527. tsk = kthread_run(audit_list, dest, "audit_list");
  528. else
  529. tsk = kthread_run(audit_list_rules, dest,
  530. "audit_list_rules");
  531. if (IS_ERR(tsk)) {
  532. kfree(dest);
  533. err = PTR_ERR(tsk);
  534. }
  535. break;
  536. case AUDIT_ADD:
  537. case AUDIT_ADD_RULE:
  538. if (type == AUDIT_ADD)
  539. entry = audit_rule_to_entry(data);
  540. else
  541. entry = audit_data_to_entry(data, datasz);
  542. if (IS_ERR(entry))
  543. return PTR_ERR(entry);
  544. err = audit_add_rule(entry,
  545. &audit_filter_list[entry->rule.listnr]);
  546. if (sid) {
  547. char *ctx = NULL;
  548. u32 len;
  549. if (selinux_ctxid_to_string(sid, &ctx, &len)) {
  550. /* Maybe call audit_panic? */
  551. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  552. "auid=%u ssid=%u add rule to list=%d res=%d",
  553. loginuid, sid, entry->rule.listnr, !err);
  554. } else
  555. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  556. "auid=%u subj=%s add rule to list=%d res=%d",
  557. loginuid, ctx, entry->rule.listnr, !err);
  558. kfree(ctx);
  559. } else
  560. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  561. "auid=%u add rule to list=%d res=%d",
  562. loginuid, entry->rule.listnr, !err);
  563. if (err)
  564. audit_free_rule(entry);
  565. break;
  566. case AUDIT_DEL:
  567. case AUDIT_DEL_RULE:
  568. if (type == AUDIT_DEL)
  569. entry = audit_rule_to_entry(data);
  570. else
  571. entry = audit_data_to_entry(data, datasz);
  572. if (IS_ERR(entry))
  573. return PTR_ERR(entry);
  574. err = audit_del_rule(entry,
  575. &audit_filter_list[entry->rule.listnr]);
  576. if (sid) {
  577. char *ctx = NULL;
  578. u32 len;
  579. if (selinux_ctxid_to_string(sid, &ctx, &len)) {
  580. /* Maybe call audit_panic? */
  581. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  582. "auid=%u ssid=%u remove rule from list=%d res=%d",
  583. loginuid, sid, entry->rule.listnr, !err);
  584. } else
  585. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  586. "auid=%u subj=%s remove rule from list=%d res=%d",
  587. loginuid, ctx, entry->rule.listnr, !err);
  588. kfree(ctx);
  589. } else
  590. audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
  591. "auid=%u remove rule from list=%d res=%d",
  592. loginuid, entry->rule.listnr, !err);
  593. audit_free_rule(entry);
  594. break;
  595. default:
  596. return -EINVAL;
  597. }
  598. return err;
  599. }
  600. int audit_comparator(const u32 left, const u32 op, const u32 right)
  601. {
  602. switch (op) {
  603. case AUDIT_EQUAL:
  604. return (left == right);
  605. case AUDIT_NOT_EQUAL:
  606. return (left != right);
  607. case AUDIT_LESS_THAN:
  608. return (left < right);
  609. case AUDIT_LESS_THAN_OR_EQUAL:
  610. return (left <= right);
  611. case AUDIT_GREATER_THAN:
  612. return (left > right);
  613. case AUDIT_GREATER_THAN_OR_EQUAL:
  614. return (left >= right);
  615. }
  616. BUG();
  617. return 0;
  618. }
  619. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  620. struct audit_krule *rule,
  621. enum audit_state *state)
  622. {
  623. int i;
  624. for (i = 0; i < rule->field_count; i++) {
  625. struct audit_field *f = &rule->fields[i];
  626. int result = 0;
  627. switch (f->type) {
  628. case AUDIT_PID:
  629. result = audit_comparator(cb->creds.pid, f->op, f->val);
  630. break;
  631. case AUDIT_UID:
  632. result = audit_comparator(cb->creds.uid, f->op, f->val);
  633. break;
  634. case AUDIT_GID:
  635. result = audit_comparator(cb->creds.gid, f->op, f->val);
  636. break;
  637. case AUDIT_LOGINUID:
  638. result = audit_comparator(cb->loginuid, f->op, f->val);
  639. break;
  640. }
  641. if (!result)
  642. return 0;
  643. }
  644. switch (rule->action) {
  645. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  646. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  647. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  648. }
  649. return 1;
  650. }
  651. int audit_filter_user(struct netlink_skb_parms *cb, int type)
  652. {
  653. struct audit_entry *e;
  654. enum audit_state state;
  655. int ret = 1;
  656. rcu_read_lock();
  657. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  658. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  659. if (state == AUDIT_DISABLED)
  660. ret = 0;
  661. break;
  662. }
  663. }
  664. rcu_read_unlock();
  665. return ret; /* Audit by default */
  666. }
  667. int audit_filter_type(int type)
  668. {
  669. struct audit_entry *e;
  670. int result = 0;
  671. rcu_read_lock();
  672. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  673. goto unlock_and_return;
  674. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  675. list) {
  676. int i;
  677. for (i = 0; i < e->rule.field_count; i++) {
  678. struct audit_field *f = &e->rule.fields[i];
  679. if (f->type == AUDIT_MSGTYPE) {
  680. result = audit_comparator(type, f->op, f->val);
  681. if (!result)
  682. break;
  683. }
  684. }
  685. if (result)
  686. goto unlock_and_return;
  687. }
  688. unlock_and_return:
  689. rcu_read_unlock();
  690. return result;
  691. }
  692. /* Check to see if the rule contains any selinux fields. Returns 1 if there
  693. are selinux fields specified in the rule, 0 otherwise. */
  694. static inline int audit_rule_has_selinux(struct audit_krule *rule)
  695. {
  696. int i;
  697. for (i = 0; i < rule->field_count; i++) {
  698. struct audit_field *f = &rule->fields[i];
  699. switch (f->type) {
  700. case AUDIT_SE_USER:
  701. case AUDIT_SE_ROLE:
  702. case AUDIT_SE_TYPE:
  703. case AUDIT_SE_SEN:
  704. case AUDIT_SE_CLR:
  705. return 1;
  706. }
  707. }
  708. return 0;
  709. }
  710. /* This function will re-initialize the se_rule field of all applicable rules.
  711. * It will traverse the filter lists serarching for rules that contain selinux
  712. * specific filter fields. When such a rule is found, it is copied, the
  713. * selinux field is re-initialized, and the old rule is replaced with the
  714. * updated rule. */
  715. int selinux_audit_rule_update(void)
  716. {
  717. struct audit_entry *entry, *n, *nentry;
  718. int i, err = 0;
  719. /* audit_netlink_mutex synchronizes the writers */
  720. mutex_lock(&audit_netlink_mutex);
  721. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  722. list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
  723. if (!audit_rule_has_selinux(&entry->rule))
  724. continue;
  725. nentry = audit_dupe_rule(&entry->rule);
  726. if (unlikely(IS_ERR(nentry))) {
  727. /* save the first error encountered for the
  728. * return value */
  729. if (!err)
  730. err = PTR_ERR(nentry);
  731. audit_panic("error updating selinux filters");
  732. list_del_rcu(&entry->list);
  733. } else {
  734. list_replace_rcu(&entry->list, &nentry->list);
  735. }
  736. call_rcu(&entry->rcu, audit_free_rule_rcu);
  737. }
  738. }
  739. mutex_unlock(&audit_netlink_mutex);
  740. return err;
  741. }