auditfilter.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. err = -EINVAL;
  410. f->op = audit_to_op(data->fieldflags[i]);
  411. if (f->op == Audit_bad)
  412. goto exit_free;
  413. f->type = data->fields[i];
  414. f->val = data->values[i];
  415. /* Support legacy tests for a valid loginuid */
  416. if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) {
  417. f->type = AUDIT_LOGINUID_SET;
  418. f->val = 0;
  419. entry->rule.pflags |= AUDIT_LOGINUID_LEGACY;
  420. }
  421. err = audit_field_valid(entry, f);
  422. if (err)
  423. goto exit_free;
  424. err = -EINVAL;
  425. switch (f->type) {
  426. case AUDIT_LOGINUID:
  427. case AUDIT_UID:
  428. case AUDIT_EUID:
  429. case AUDIT_SUID:
  430. case AUDIT_FSUID:
  431. case AUDIT_OBJ_UID:
  432. f->uid = make_kuid(current_user_ns(), f->val);
  433. if (!uid_valid(f->uid))
  434. goto exit_free;
  435. break;
  436. case AUDIT_GID:
  437. case AUDIT_EGID:
  438. case AUDIT_SGID:
  439. case AUDIT_FSGID:
  440. case AUDIT_OBJ_GID:
  441. f->gid = make_kgid(current_user_ns(), f->val);
  442. if (!gid_valid(f->gid))
  443. goto exit_free;
  444. break;
  445. case AUDIT_ARCH:
  446. entry->rule.arch_f = f;
  447. break;
  448. case AUDIT_SUBJ_USER:
  449. case AUDIT_SUBJ_ROLE:
  450. case AUDIT_SUBJ_TYPE:
  451. case AUDIT_SUBJ_SEN:
  452. case AUDIT_SUBJ_CLR:
  453. case AUDIT_OBJ_USER:
  454. case AUDIT_OBJ_ROLE:
  455. case AUDIT_OBJ_TYPE:
  456. case AUDIT_OBJ_LEV_LOW:
  457. case AUDIT_OBJ_LEV_HIGH:
  458. str = audit_unpack_string(&bufp, &remain, f->val);
  459. if (IS_ERR(str))
  460. goto exit_free;
  461. entry->rule.buflen += f->val;
  462. err = security_audit_rule_init(f->type, f->op, str,
  463. (void **)&f->lsm_rule);
  464. /* Keep currently invalid fields around in case they
  465. * become valid after a policy reload. */
  466. if (err == -EINVAL) {
  467. pr_warn("audit rule for LSM \'%s\' is invalid\n",
  468. str);
  469. err = 0;
  470. }
  471. if (err) {
  472. kfree(str);
  473. goto exit_free;
  474. } else
  475. f->lsm_str = str;
  476. break;
  477. case AUDIT_WATCH:
  478. str = audit_unpack_string(&bufp, &remain, f->val);
  479. if (IS_ERR(str))
  480. goto exit_free;
  481. entry->rule.buflen += f->val;
  482. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  483. if (err) {
  484. kfree(str);
  485. goto exit_free;
  486. }
  487. break;
  488. case AUDIT_DIR:
  489. str = audit_unpack_string(&bufp, &remain, f->val);
  490. if (IS_ERR(str))
  491. goto exit_free;
  492. entry->rule.buflen += f->val;
  493. err = audit_make_tree(&entry->rule, str, f->op);
  494. kfree(str);
  495. if (err)
  496. goto exit_free;
  497. break;
  498. case AUDIT_INODE:
  499. err = audit_to_inode(&entry->rule, f);
  500. if (err)
  501. goto exit_free;
  502. break;
  503. case AUDIT_FILTERKEY:
  504. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  505. goto exit_free;
  506. str = audit_unpack_string(&bufp, &remain, f->val);
  507. if (IS_ERR(str))
  508. goto exit_free;
  509. entry->rule.buflen += f->val;
  510. entry->rule.filterkey = str;
  511. break;
  512. case AUDIT_EXE:
  513. if (entry->rule.exe || f->val > PATH_MAX)
  514. goto exit_free;
  515. str = audit_unpack_string(&bufp, &remain, f->val);
  516. if (IS_ERR(str)) {
  517. err = PTR_ERR(str);
  518. goto exit_free;
  519. }
  520. entry->rule.buflen += f->val;
  521. audit_mark = audit_alloc_mark(&entry->rule, str, f->val);
  522. if (IS_ERR(audit_mark)) {
  523. kfree(str);
  524. err = PTR_ERR(audit_mark);
  525. goto exit_free;
  526. }
  527. entry->rule.exe = audit_mark;
  528. break;
  529. }
  530. }
  531. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  532. entry->rule.inode_f = NULL;
  533. exit_nofree:
  534. return entry;
  535. exit_free:
  536. if (entry->rule.tree)
  537. audit_put_tree(entry->rule.tree); /* that's the temporary one */
  538. if (entry->rule.exe)
  539. audit_remove_mark(entry->rule.exe); /* that's the template one */
  540. audit_free_rule(entry);
  541. return ERR_PTR(err);
  542. }
  543. /* Pack a filter field's string representation into data block. */
  544. static inline size_t audit_pack_string(void **bufp, const char *str)
  545. {
  546. size_t len = strlen(str);
  547. memcpy(*bufp, str, len);
  548. *bufp += len;
  549. return len;
  550. }
  551. /* Translate kernel rule representation to struct audit_rule_data. */
  552. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  553. {
  554. struct audit_rule_data *data;
  555. void *bufp;
  556. int i;
  557. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  558. if (unlikely(!data))
  559. return NULL;
  560. memset(data, 0, sizeof(*data));
  561. data->flags = krule->flags | krule->listnr;
  562. data->action = krule->action;
  563. data->field_count = krule->field_count;
  564. bufp = data->buf;
  565. for (i = 0; i < data->field_count; i++) {
  566. struct audit_field *f = &krule->fields[i];
  567. data->fields[i] = f->type;
  568. data->fieldflags[i] = audit_ops[f->op];
  569. switch(f->type) {
  570. case AUDIT_SUBJ_USER:
  571. case AUDIT_SUBJ_ROLE:
  572. case AUDIT_SUBJ_TYPE:
  573. case AUDIT_SUBJ_SEN:
  574. case AUDIT_SUBJ_CLR:
  575. case AUDIT_OBJ_USER:
  576. case AUDIT_OBJ_ROLE:
  577. case AUDIT_OBJ_TYPE:
  578. case AUDIT_OBJ_LEV_LOW:
  579. case AUDIT_OBJ_LEV_HIGH:
  580. data->buflen += data->values[i] =
  581. audit_pack_string(&bufp, f->lsm_str);
  582. break;
  583. case AUDIT_WATCH:
  584. data->buflen += data->values[i] =
  585. audit_pack_string(&bufp,
  586. audit_watch_path(krule->watch));
  587. break;
  588. case AUDIT_DIR:
  589. data->buflen += data->values[i] =
  590. audit_pack_string(&bufp,
  591. audit_tree_path(krule->tree));
  592. break;
  593. case AUDIT_FILTERKEY:
  594. data->buflen += data->values[i] =
  595. audit_pack_string(&bufp, krule->filterkey);
  596. break;
  597. case AUDIT_EXE:
  598. data->buflen += data->values[i] =
  599. audit_pack_string(&bufp, audit_mark_path(krule->exe));
  600. break;
  601. case AUDIT_LOGINUID_SET:
  602. if (krule->pflags & AUDIT_LOGINUID_LEGACY && !f->val) {
  603. data->fields[i] = AUDIT_LOGINUID;
  604. data->values[i] = AUDIT_UID_UNSET;
  605. break;
  606. }
  607. /* fallthrough if set */
  608. default:
  609. data->values[i] = f->val;
  610. }
  611. }
  612. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  613. return data;
  614. }
  615. /* Compare two rules in kernel format. Considered success if rules
  616. * don't match. */
  617. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  618. {
  619. int i;
  620. if (a->flags != b->flags ||
  621. a->pflags != b->pflags ||
  622. a->listnr != b->listnr ||
  623. a->action != b->action ||
  624. a->field_count != b->field_count)
  625. return 1;
  626. for (i = 0; i < a->field_count; i++) {
  627. if (a->fields[i].type != b->fields[i].type ||
  628. a->fields[i].op != b->fields[i].op)
  629. return 1;
  630. switch(a->fields[i].type) {
  631. case AUDIT_SUBJ_USER:
  632. case AUDIT_SUBJ_ROLE:
  633. case AUDIT_SUBJ_TYPE:
  634. case AUDIT_SUBJ_SEN:
  635. case AUDIT_SUBJ_CLR:
  636. case AUDIT_OBJ_USER:
  637. case AUDIT_OBJ_ROLE:
  638. case AUDIT_OBJ_TYPE:
  639. case AUDIT_OBJ_LEV_LOW:
  640. case AUDIT_OBJ_LEV_HIGH:
  641. if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
  642. return 1;
  643. break;
  644. case AUDIT_WATCH:
  645. if (strcmp(audit_watch_path(a->watch),
  646. audit_watch_path(b->watch)))
  647. return 1;
  648. break;
  649. case AUDIT_DIR:
  650. if (strcmp(audit_tree_path(a->tree),
  651. audit_tree_path(b->tree)))
  652. return 1;
  653. break;
  654. case AUDIT_FILTERKEY:
  655. /* both filterkeys exist based on above type compare */
  656. if (strcmp(a->filterkey, b->filterkey))
  657. return 1;
  658. break;
  659. case AUDIT_EXE:
  660. /* both paths exist based on above type compare */
  661. if (strcmp(audit_mark_path(a->exe),
  662. audit_mark_path(b->exe)))
  663. return 1;
  664. break;
  665. case AUDIT_UID:
  666. case AUDIT_EUID:
  667. case AUDIT_SUID:
  668. case AUDIT_FSUID:
  669. case AUDIT_LOGINUID:
  670. case AUDIT_OBJ_UID:
  671. if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
  672. return 1;
  673. break;
  674. case AUDIT_GID:
  675. case AUDIT_EGID:
  676. case AUDIT_SGID:
  677. case AUDIT_FSGID:
  678. case AUDIT_OBJ_GID:
  679. if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
  680. return 1;
  681. break;
  682. default:
  683. if (a->fields[i].val != b->fields[i].val)
  684. return 1;
  685. }
  686. }
  687. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  688. if (a->mask[i] != b->mask[i])
  689. return 1;
  690. return 0;
  691. }
  692. /* Duplicate LSM field information. The lsm_rule is opaque, so must be
  693. * re-initialized. */
  694. static inline int audit_dupe_lsm_field(struct audit_field *df,
  695. struct audit_field *sf)
  696. {
  697. int ret = 0;
  698. char *lsm_str;
  699. /* our own copy of lsm_str */
  700. lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
  701. if (unlikely(!lsm_str))
  702. return -ENOMEM;
  703. df->lsm_str = lsm_str;
  704. /* our own (refreshed) copy of lsm_rule */
  705. ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
  706. (void **)&df->lsm_rule);
  707. /* Keep currently invalid fields around in case they
  708. * become valid after a policy reload. */
  709. if (ret == -EINVAL) {
  710. pr_warn("audit rule for LSM \'%s\' is invalid\n",
  711. df->lsm_str);
  712. ret = 0;
  713. }
  714. return ret;
  715. }
  716. /* Duplicate an audit rule. This will be a deep copy with the exception
  717. * of the watch - that pointer is carried over. The LSM specific fields
  718. * will be updated in the copy. The point is to be able to replace the old
  719. * rule with the new rule in the filterlist, then free the old rule.
  720. * The rlist element is undefined; list manipulations are handled apart from
  721. * the initial copy. */
  722. struct audit_entry *audit_dupe_rule(struct audit_krule *old)
  723. {
  724. u32 fcount = old->field_count;
  725. struct audit_entry *entry;
  726. struct audit_krule *new;
  727. char *fk;
  728. int i, err = 0;
  729. entry = audit_init_entry(fcount);
  730. if (unlikely(!entry))
  731. return ERR_PTR(-ENOMEM);
  732. new = &entry->rule;
  733. new->flags = old->flags;
  734. new->pflags = old->pflags;
  735. new->listnr = old->listnr;
  736. new->action = old->action;
  737. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  738. new->mask[i] = old->mask[i];
  739. new->prio = old->prio;
  740. new->buflen = old->buflen;
  741. new->inode_f = old->inode_f;
  742. new->field_count = old->field_count;
  743. /*
  744. * note that we are OK with not refcounting here; audit_match_tree()
  745. * never dereferences tree and we can't get false positives there
  746. * since we'd have to have rule gone from the list *and* removed
  747. * before the chunks found by lookup had been allocated, i.e. before
  748. * the beginning of list scan.
  749. */
  750. new->tree = old->tree;
  751. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  752. /* deep copy this information, updating the lsm_rule fields, because
  753. * the originals will all be freed when the old rule is freed. */
  754. for (i = 0; i < fcount; i++) {
  755. switch (new->fields[i].type) {
  756. case AUDIT_SUBJ_USER:
  757. case AUDIT_SUBJ_ROLE:
  758. case AUDIT_SUBJ_TYPE:
  759. case AUDIT_SUBJ_SEN:
  760. case AUDIT_SUBJ_CLR:
  761. case AUDIT_OBJ_USER:
  762. case AUDIT_OBJ_ROLE:
  763. case AUDIT_OBJ_TYPE:
  764. case AUDIT_OBJ_LEV_LOW:
  765. case AUDIT_OBJ_LEV_HIGH:
  766. err = audit_dupe_lsm_field(&new->fields[i],
  767. &old->fields[i]);
  768. break;
  769. case AUDIT_FILTERKEY:
  770. fk = kstrdup(old->filterkey, GFP_KERNEL);
  771. if (unlikely(!fk))
  772. err = -ENOMEM;
  773. else
  774. new->filterkey = fk;
  775. break;
  776. case AUDIT_EXE:
  777. err = audit_dupe_exe(new, old);
  778. break;
  779. }
  780. if (err) {
  781. if (new->exe)
  782. audit_remove_mark(new->exe);
  783. audit_free_rule(entry);
  784. return ERR_PTR(err);
  785. }
  786. }
  787. if (old->watch) {
  788. audit_get_watch(old->watch);
  789. new->watch = old->watch;
  790. }
  791. return entry;
  792. }
  793. /* Find an existing audit rule.
  794. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  795. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  796. struct list_head **p)
  797. {
  798. struct audit_entry *e, *found = NULL;
  799. struct list_head *list;
  800. int h;
  801. if (entry->rule.inode_f) {
  802. h = audit_hash_ino(entry->rule.inode_f->val);
  803. *p = list = &audit_inode_hash[h];
  804. } else if (entry->rule.watch) {
  805. /* we don't know the inode number, so must walk entire hash */
  806. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  807. list = &audit_inode_hash[h];
  808. list_for_each_entry(e, list, list)
  809. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  810. found = e;
  811. goto out;
  812. }
  813. }
  814. goto out;
  815. } else {
  816. *p = list = &audit_filter_list[entry->rule.listnr];
  817. }
  818. list_for_each_entry(e, list, list)
  819. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  820. found = e;
  821. goto out;
  822. }
  823. out:
  824. return found;
  825. }
  826. static u64 prio_low = ~0ULL/2;
  827. static u64 prio_high = ~0ULL/2 - 1;
  828. /* Add rule to given filterlist if not a duplicate. */
  829. static inline int audit_add_rule(struct audit_entry *entry)
  830. {
  831. struct audit_entry *e;
  832. struct audit_watch *watch = entry->rule.watch;
  833. struct audit_tree *tree = entry->rule.tree;
  834. struct list_head *list;
  835. int err = 0;
  836. #ifdef CONFIG_AUDITSYSCALL
  837. int dont_count = 0;
  838. /* If any of these, don't count towards total */
  839. switch(entry->rule.listnr) {
  840. case AUDIT_FILTER_USER:
  841. case AUDIT_FILTER_EXCLUDE:
  842. case AUDIT_FILTER_FS:
  843. dont_count = 1;
  844. }
  845. #endif
  846. mutex_lock(&audit_filter_mutex);
  847. e = audit_find_rule(entry, &list);
  848. if (e) {
  849. mutex_unlock(&audit_filter_mutex);
  850. err = -EEXIST;
  851. /* normally audit_add_tree_rule() will free it on failure */
  852. if (tree)
  853. audit_put_tree(tree);
  854. return err;
  855. }
  856. if (watch) {
  857. /* audit_filter_mutex is dropped and re-taken during this call */
  858. err = audit_add_watch(&entry->rule, &list);
  859. if (err) {
  860. mutex_unlock(&audit_filter_mutex);
  861. /*
  862. * normally audit_add_tree_rule() will free it
  863. * on failure
  864. */
  865. if (tree)
  866. audit_put_tree(tree);
  867. return err;
  868. }
  869. }
  870. if (tree) {
  871. err = audit_add_tree_rule(&entry->rule);
  872. if (err) {
  873. mutex_unlock(&audit_filter_mutex);
  874. return err;
  875. }
  876. }
  877. entry->rule.prio = ~0ULL;
  878. if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
  879. if (entry->rule.flags & AUDIT_FILTER_PREPEND)
  880. entry->rule.prio = ++prio_high;
  881. else
  882. entry->rule.prio = --prio_low;
  883. }
  884. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  885. list_add(&entry->rule.list,
  886. &audit_rules_list[entry->rule.listnr]);
  887. list_add_rcu(&entry->list, list);
  888. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  889. } else {
  890. list_add_tail(&entry->rule.list,
  891. &audit_rules_list[entry->rule.listnr]);
  892. list_add_tail_rcu(&entry->list, list);
  893. }
  894. #ifdef CONFIG_AUDITSYSCALL
  895. if (!dont_count)
  896. audit_n_rules++;
  897. if (!audit_match_signal(entry))
  898. audit_signals++;
  899. #endif
  900. mutex_unlock(&audit_filter_mutex);
  901. return err;
  902. }
  903. /* Remove an existing rule from filterlist. */
  904. int audit_del_rule(struct audit_entry *entry)
  905. {
  906. struct audit_entry *e;
  907. struct audit_tree *tree = entry->rule.tree;
  908. struct list_head *list;
  909. int ret = 0;
  910. #ifdef CONFIG_AUDITSYSCALL
  911. int dont_count = 0;
  912. /* If any of these, don't count towards total */
  913. switch(entry->rule.listnr) {
  914. case AUDIT_FILTER_USER:
  915. case AUDIT_FILTER_EXCLUDE:
  916. case AUDIT_FILTER_FS:
  917. dont_count = 1;
  918. }
  919. #endif
  920. mutex_lock(&audit_filter_mutex);
  921. e = audit_find_rule(entry, &list);
  922. if (!e) {
  923. ret = -ENOENT;
  924. goto out;
  925. }
  926. if (e->rule.watch)
  927. audit_remove_watch_rule(&e->rule);
  928. if (e->rule.tree)
  929. audit_remove_tree_rule(&e->rule);
  930. if (e->rule.exe)
  931. audit_remove_mark_rule(&e->rule);
  932. #ifdef CONFIG_AUDITSYSCALL
  933. if (!dont_count)
  934. audit_n_rules--;
  935. if (!audit_match_signal(entry))
  936. audit_signals--;
  937. #endif
  938. list_del_rcu(&e->list);
  939. list_del(&e->rule.list);
  940. call_rcu(&e->rcu, audit_free_rule_rcu);
  941. out:
  942. mutex_unlock(&audit_filter_mutex);
  943. if (tree)
  944. audit_put_tree(tree); /* that's the temporary one */
  945. return ret;
  946. }
  947. /* List rules using struct audit_rule_data. */
  948. static void audit_list_rules(int seq, struct sk_buff_head *q)
  949. {
  950. struct sk_buff *skb;
  951. struct audit_krule *r;
  952. int i;
  953. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  954. * iterator to sync with list writers. */
  955. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  956. list_for_each_entry(r, &audit_rules_list[i], list) {
  957. struct audit_rule_data *data;
  958. data = audit_krule_to_data(r);
  959. if (unlikely(!data))
  960. break;
  961. skb = audit_make_reply(seq, AUDIT_LIST_RULES, 0, 1,
  962. data,
  963. sizeof(*data) + data->buflen);
  964. if (skb)
  965. skb_queue_tail(q, skb);
  966. kfree(data);
  967. }
  968. }
  969. skb = audit_make_reply(seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  970. if (skb)
  971. skb_queue_tail(q, skb);
  972. }
  973. /* Log rule additions and removals */
  974. static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
  975. {
  976. struct audit_buffer *ab;
  977. if (!audit_enabled)
  978. return;
  979. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  980. if (!ab)
  981. return;
  982. audit_log_session_info(ab);
  983. audit_log_task_context(ab);
  984. audit_log_format(ab, " op=%s", action);
  985. audit_log_key(ab, rule->filterkey);
  986. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  987. audit_log_end(ab);
  988. }
  989. /**
  990. * audit_rule_change - apply all rules to the specified message type
  991. * @type: audit message type
  992. * @seq: netlink audit message sequence (serial) number
  993. * @data: payload data
  994. * @datasz: size of payload data
  995. */
  996. int audit_rule_change(int type, int seq, void *data, size_t datasz)
  997. {
  998. int err = 0;
  999. struct audit_entry *entry;
  1000. entry = audit_data_to_entry(data, datasz);
  1001. if (IS_ERR(entry))
  1002. return PTR_ERR(entry);
  1003. switch (type) {
  1004. case AUDIT_ADD_RULE:
  1005. err = audit_add_rule(entry);
  1006. audit_log_rule_change("add_rule", &entry->rule, !err);
  1007. break;
  1008. case AUDIT_DEL_RULE:
  1009. err = audit_del_rule(entry);
  1010. audit_log_rule_change("remove_rule", &entry->rule, !err);
  1011. break;
  1012. default:
  1013. err = -EINVAL;
  1014. WARN_ON(1);
  1015. }
  1016. if (err || type == AUDIT_DEL_RULE) {
  1017. if (entry->rule.exe)
  1018. audit_remove_mark(entry->rule.exe);
  1019. audit_free_rule(entry);
  1020. }
  1021. return err;
  1022. }
  1023. /**
  1024. * audit_list_rules_send - list the audit rules
  1025. * @request_skb: skb of request we are replying to (used to target the reply)
  1026. * @seq: netlink audit message sequence (serial) number
  1027. */
  1028. int audit_list_rules_send(struct sk_buff *request_skb, int seq)
  1029. {
  1030. u32 portid = NETLINK_CB(request_skb).portid;
  1031. struct net *net = sock_net(NETLINK_CB(request_skb).sk);
  1032. struct task_struct *tsk;
  1033. struct audit_netlink_list *dest;
  1034. int err = 0;
  1035. /* We can't just spew out the rules here because we might fill
  1036. * the available socket buffer space and deadlock waiting for
  1037. * auditctl to read from it... which isn't ever going to
  1038. * happen if we're actually running in the context of auditctl
  1039. * trying to _send_ the stuff */
  1040. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1041. if (!dest)
  1042. return -ENOMEM;
  1043. dest->net = get_net(net);
  1044. dest->portid = portid;
  1045. skb_queue_head_init(&dest->q);
  1046. mutex_lock(&audit_filter_mutex);
  1047. audit_list_rules(seq, &dest->q);
  1048. mutex_unlock(&audit_filter_mutex);
  1049. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1050. if (IS_ERR(tsk)) {
  1051. skb_queue_purge(&dest->q);
  1052. kfree(dest);
  1053. err = PTR_ERR(tsk);
  1054. }
  1055. return err;
  1056. }
  1057. int audit_comparator(u32 left, u32 op, u32 right)
  1058. {
  1059. switch (op) {
  1060. case Audit_equal:
  1061. return (left == right);
  1062. case Audit_not_equal:
  1063. return (left != right);
  1064. case Audit_lt:
  1065. return (left < right);
  1066. case Audit_le:
  1067. return (left <= right);
  1068. case Audit_gt:
  1069. return (left > right);
  1070. case Audit_ge:
  1071. return (left >= right);
  1072. case Audit_bitmask:
  1073. return (left & right);
  1074. case Audit_bittest:
  1075. return ((left & right) == right);
  1076. default:
  1077. BUG();
  1078. return 0;
  1079. }
  1080. }
  1081. int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
  1082. {
  1083. switch (op) {
  1084. case Audit_equal:
  1085. return uid_eq(left, right);
  1086. case Audit_not_equal:
  1087. return !uid_eq(left, right);
  1088. case Audit_lt:
  1089. return uid_lt(left, right);
  1090. case Audit_le:
  1091. return uid_lte(left, right);
  1092. case Audit_gt:
  1093. return uid_gt(left, right);
  1094. case Audit_ge:
  1095. return uid_gte(left, right);
  1096. case Audit_bitmask:
  1097. case Audit_bittest:
  1098. default:
  1099. BUG();
  1100. return 0;
  1101. }
  1102. }
  1103. int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
  1104. {
  1105. switch (op) {
  1106. case Audit_equal:
  1107. return gid_eq(left, right);
  1108. case Audit_not_equal:
  1109. return !gid_eq(left, right);
  1110. case Audit_lt:
  1111. return gid_lt(left, right);
  1112. case Audit_le:
  1113. return gid_lte(left, right);
  1114. case Audit_gt:
  1115. return gid_gt(left, right);
  1116. case Audit_ge:
  1117. return gid_gte(left, right);
  1118. case Audit_bitmask:
  1119. case Audit_bittest:
  1120. default:
  1121. BUG();
  1122. return 0;
  1123. }
  1124. }
  1125. /**
  1126. * parent_len - find the length of the parent portion of a pathname
  1127. * @path: pathname of which to determine length
  1128. */
  1129. int parent_len(const char *path)
  1130. {
  1131. int plen;
  1132. const char *p;
  1133. plen = strlen(path);
  1134. if (plen == 0)
  1135. return plen;
  1136. /* disregard trailing slashes */
  1137. p = path + plen - 1;
  1138. while ((*p == '/') && (p > path))
  1139. p--;
  1140. /* walk backward until we find the next slash or hit beginning */
  1141. while ((*p != '/') && (p > path))
  1142. p--;
  1143. /* did we find a slash? Then increment to include it in path */
  1144. if (*p == '/')
  1145. p++;
  1146. return p - path;
  1147. }
  1148. /**
  1149. * audit_compare_dname_path - compare given dentry name with last component in
  1150. * given path. Return of 0 indicates a match.
  1151. * @dname: dentry name that we're comparing
  1152. * @path: full pathname that we're comparing
  1153. * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
  1154. * here indicates that we must compute this value.
  1155. */
  1156. int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
  1157. {
  1158. int dlen, pathlen;
  1159. const char *p;
  1160. dlen = strlen(dname);
  1161. pathlen = strlen(path);
  1162. if (pathlen < dlen)
  1163. return 1;
  1164. parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
  1165. if (pathlen - parentlen != dlen)
  1166. return 1;
  1167. p = path + parentlen;
  1168. return strncmp(p, dname, dlen);
  1169. }
  1170. int audit_filter(int msgtype, unsigned int listtype)
  1171. {
  1172. struct audit_entry *e;
  1173. int ret = 1; /* Audit by default */
  1174. rcu_read_lock();
  1175. if (list_empty(&audit_filter_list[listtype]))
  1176. goto unlock_and_return;
  1177. list_for_each_entry_rcu(e, &audit_filter_list[listtype], list) {
  1178. int i, result = 0;
  1179. for (i = 0; i < e->rule.field_count; i++) {
  1180. struct audit_field *f = &e->rule.fields[i];
  1181. pid_t pid;
  1182. u32 sid;
  1183. switch (f->type) {
  1184. case AUDIT_PID:
  1185. pid = task_pid_nr(current);
  1186. result = audit_comparator(pid, f->op, f->val);
  1187. break;
  1188. case AUDIT_UID:
  1189. result = audit_uid_comparator(current_uid(), f->op, f->uid);
  1190. break;
  1191. case AUDIT_GID:
  1192. result = audit_gid_comparator(current_gid(), f->op, f->gid);
  1193. break;
  1194. case AUDIT_LOGINUID:
  1195. result = audit_uid_comparator(audit_get_loginuid(current),
  1196. f->op, f->uid);
  1197. break;
  1198. case AUDIT_LOGINUID_SET:
  1199. result = audit_comparator(audit_loginuid_set(current),
  1200. f->op, f->val);
  1201. break;
  1202. case AUDIT_MSGTYPE:
  1203. result = audit_comparator(msgtype, f->op, f->val);
  1204. break;
  1205. case AUDIT_SUBJ_USER:
  1206. case AUDIT_SUBJ_ROLE:
  1207. case AUDIT_SUBJ_TYPE:
  1208. case AUDIT_SUBJ_SEN:
  1209. case AUDIT_SUBJ_CLR:
  1210. if (f->lsm_rule) {
  1211. security_task_getsecid(current, &sid);
  1212. result = security_audit_rule_match(sid,
  1213. f->type, f->op, f->lsm_rule, NULL);
  1214. }
  1215. break;
  1216. case AUDIT_EXE:
  1217. result = audit_exe_compare(current, e->rule.exe);
  1218. if (f->op == Audit_not_equal)
  1219. result = !result;
  1220. break;
  1221. default:
  1222. goto unlock_and_return;
  1223. }
  1224. if (result < 0) /* error */
  1225. goto unlock_and_return;
  1226. if (!result)
  1227. break;
  1228. }
  1229. if (result > 0) {
  1230. if (e->rule.action == AUDIT_NEVER || listtype == AUDIT_FILTER_EXCLUDE)
  1231. ret = 0;
  1232. break;
  1233. }
  1234. }
  1235. unlock_and_return:
  1236. rcu_read_unlock();
  1237. return ret;
  1238. }
  1239. static int update_lsm_rule(struct audit_krule *r)
  1240. {
  1241. struct audit_entry *entry = container_of(r, struct audit_entry, rule);
  1242. struct audit_entry *nentry;
  1243. int err = 0;
  1244. if (!security_audit_rule_known(r))
  1245. return 0;
  1246. nentry = audit_dupe_rule(r);
  1247. if (entry->rule.exe)
  1248. audit_remove_mark(entry->rule.exe);
  1249. if (IS_ERR(nentry)) {
  1250. /* save the first error encountered for the
  1251. * return value */
  1252. err = PTR_ERR(nentry);
  1253. audit_panic("error updating LSM filters");
  1254. if (r->watch)
  1255. list_del(&r->rlist);
  1256. list_del_rcu(&entry->list);
  1257. list_del(&r->list);
  1258. } else {
  1259. if (r->watch || r->tree)
  1260. list_replace_init(&r->rlist, &nentry->rule.rlist);
  1261. list_replace_rcu(&entry->list, &nentry->list);
  1262. list_replace(&r->list, &nentry->rule.list);
  1263. }
  1264. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1265. return err;
  1266. }
  1267. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1268. * It will traverse the filter lists serarching for rules that contain LSM
  1269. * specific filter fields. When such a rule is found, it is copied, the
  1270. * LSM field is re-initialized, and the old rule is replaced with the
  1271. * updated rule. */
  1272. int audit_update_lsm_rules(void)
  1273. {
  1274. struct audit_krule *r, *n;
  1275. int i, err = 0;
  1276. /* audit_filter_mutex synchronizes the writers */
  1277. mutex_lock(&audit_filter_mutex);
  1278. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1279. list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
  1280. int res = update_lsm_rule(r);
  1281. if (!err)
  1282. err = res;
  1283. }
  1284. }
  1285. mutex_unlock(&audit_filter_mutex);
  1286. return err;
  1287. }