auditfilter.c 34 KB

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