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