auditfilter.c 36 KB

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