auditfilter.c 34 KB

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