auditfilter.c 21 KB

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