auditfilter.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /* auditfilter.c -- filtering of audit events
  2. *
  3. * Copyright 2003-2004 Red Hat, Inc.
  4. * Copyright 2005 Hewlett-Packard Development Company, L.P.
  5. * Copyright 2005 IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/audit.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/fs.h>
  26. #include <linux/namei.h>
  27. #include <linux/netlink.h>
  28. #include <linux/sched.h>
  29. #include <linux/inotify.h>
  30. #include <linux/security.h>
  31. #include "audit.h"
  32. /*
  33. * Locking model:
  34. *
  35. * audit_filter_mutex:
  36. * Synchronizes writes and blocking reads of audit's filterlist
  37. * data. Rcu is used to traverse the filterlist and access
  38. * contents of structs audit_entry, audit_watch and opaque
  39. * LSM rules during filtering. If modified, these structures
  40. * must be copied and replace their counterparts in the filterlist.
  41. * An audit_parent struct is not accessed during filtering, so may
  42. * be written directly provided audit_filter_mutex is held.
  43. */
  44. /*
  45. * Reference counting:
  46. *
  47. * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
  48. * event. Each audit_watch holds a reference to its associated parent.
  49. *
  50. * audit_watch: if added to lists, lifetime is from audit_init_watch() to
  51. * audit_remove_watch(). Additionally, an audit_watch may exist
  52. * temporarily to assist in searching existing filter data. Each
  53. * audit_krule holds a reference to its associated watch.
  54. */
  55. struct audit_parent {
  56. struct list_head ilist; /* entry in inotify registration list */
  57. struct list_head watches; /* associated watches */
  58. struct inotify_watch wdata; /* inotify watch data */
  59. unsigned flags; /* status flags */
  60. };
  61. /*
  62. * audit_parent status flags:
  63. *
  64. * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
  65. * a filesystem event to ensure we're adding audit watches to a valid parent.
  66. * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
  67. * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
  68. * we can receive while holding nameidata.
  69. */
  70. #define AUDIT_PARENT_INVALID 0x001
  71. /* Audit filter lists, defined in <linux/audit.h> */
  72. struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  73. LIST_HEAD_INIT(audit_filter_list[0]),
  74. LIST_HEAD_INIT(audit_filter_list[1]),
  75. LIST_HEAD_INIT(audit_filter_list[2]),
  76. LIST_HEAD_INIT(audit_filter_list[3]),
  77. LIST_HEAD_INIT(audit_filter_list[4]),
  78. LIST_HEAD_INIT(audit_filter_list[5]),
  79. #if AUDIT_NR_FILTERS != 6
  80. #error Fix audit_filter_list initialiser
  81. #endif
  82. };
  83. DEFINE_MUTEX(audit_filter_mutex);
  84. /* Inotify events we care about. */
  85. #define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
  86. void audit_free_parent(struct inotify_watch *i_watch)
  87. {
  88. struct audit_parent *parent;
  89. parent = container_of(i_watch, struct audit_parent, wdata);
  90. WARN_ON(!list_empty(&parent->watches));
  91. kfree(parent);
  92. }
  93. static inline void audit_get_watch(struct audit_watch *watch)
  94. {
  95. atomic_inc(&watch->count);
  96. }
  97. static void audit_put_watch(struct audit_watch *watch)
  98. {
  99. if (atomic_dec_and_test(&watch->count)) {
  100. WARN_ON(watch->parent);
  101. WARN_ON(!list_empty(&watch->rules));
  102. kfree(watch->path);
  103. kfree(watch);
  104. }
  105. }
  106. static void audit_remove_watch(struct audit_watch *watch)
  107. {
  108. list_del(&watch->wlist);
  109. put_inotify_watch(&watch->parent->wdata);
  110. watch->parent = NULL;
  111. audit_put_watch(watch); /* match initial get */
  112. }
  113. static inline void audit_free_rule(struct audit_entry *e)
  114. {
  115. int i;
  116. /* some rules don't have associated watches */
  117. if (e->rule.watch)
  118. audit_put_watch(e->rule.watch);
  119. if (e->rule.fields)
  120. for (i = 0; i < e->rule.field_count; i++) {
  121. struct audit_field *f = &e->rule.fields[i];
  122. kfree(f->lsm_str);
  123. security_audit_rule_free(f->lsm_rule);
  124. }
  125. kfree(e->rule.fields);
  126. kfree(e->rule.filterkey);
  127. kfree(e);
  128. }
  129. void audit_free_rule_rcu(struct rcu_head *head)
  130. {
  131. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  132. audit_free_rule(e);
  133. }
  134. /* Initialize a parent watch entry. */
  135. static struct audit_parent *audit_init_parent(struct nameidata *ndp)
  136. {
  137. struct audit_parent *parent;
  138. s32 wd;
  139. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  140. if (unlikely(!parent))
  141. return ERR_PTR(-ENOMEM);
  142. INIT_LIST_HEAD(&parent->watches);
  143. parent->flags = 0;
  144. inotify_init_watch(&parent->wdata);
  145. /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
  146. get_inotify_watch(&parent->wdata);
  147. wd = inotify_add_watch(audit_ih, &parent->wdata,
  148. ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
  149. if (wd < 0) {
  150. audit_free_parent(&parent->wdata);
  151. return ERR_PTR(wd);
  152. }
  153. return parent;
  154. }
  155. /* Initialize a watch entry. */
  156. static struct audit_watch *audit_init_watch(char *path)
  157. {
  158. struct audit_watch *watch;
  159. watch = kzalloc(sizeof(*watch), GFP_KERNEL);
  160. if (unlikely(!watch))
  161. return ERR_PTR(-ENOMEM);
  162. INIT_LIST_HEAD(&watch->rules);
  163. atomic_set(&watch->count, 1);
  164. watch->path = path;
  165. watch->dev = (dev_t)-1;
  166. watch->ino = (unsigned long)-1;
  167. return watch;
  168. }
  169. /* Initialize an audit filterlist entry. */
  170. static inline struct audit_entry *audit_init_entry(u32 field_count)
  171. {
  172. struct audit_entry *entry;
  173. struct audit_field *fields;
  174. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  175. if (unlikely(!entry))
  176. return NULL;
  177. fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
  178. if (unlikely(!fields)) {
  179. kfree(entry);
  180. return NULL;
  181. }
  182. entry->rule.fields = fields;
  183. return entry;
  184. }
  185. /* Unpack a filter field's string representation from user-space
  186. * buffer. */
  187. char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
  188. {
  189. char *str;
  190. if (!*bufp || (len == 0) || (len > *remain))
  191. return ERR_PTR(-EINVAL);
  192. /* Of the currently implemented string fields, PATH_MAX
  193. * defines the longest valid length.
  194. */
  195. if (len > PATH_MAX)
  196. return ERR_PTR(-ENAMETOOLONG);
  197. str = kmalloc(len + 1, GFP_KERNEL);
  198. if (unlikely(!str))
  199. return ERR_PTR(-ENOMEM);
  200. memcpy(str, *bufp, len);
  201. str[len] = 0;
  202. *bufp += len;
  203. *remain -= len;
  204. return str;
  205. }
  206. /* Translate an inode field to kernel respresentation. */
  207. static inline int audit_to_inode(struct audit_krule *krule,
  208. struct audit_field *f)
  209. {
  210. if (krule->listnr != AUDIT_FILTER_EXIT ||
  211. krule->watch || krule->inode_f || krule->tree)
  212. return -EINVAL;
  213. krule->inode_f = f;
  214. return 0;
  215. }
  216. /* Translate a watch string to kernel respresentation. */
  217. static int audit_to_watch(struct audit_krule *krule, char *path, int len,
  218. u32 op)
  219. {
  220. struct audit_watch *watch;
  221. if (!audit_ih)
  222. return -EOPNOTSUPP;
  223. if (path[0] != '/' || path[len-1] == '/' ||
  224. krule->listnr != AUDIT_FILTER_EXIT ||
  225. op & ~AUDIT_EQUAL ||
  226. krule->inode_f || krule->watch || krule->tree)
  227. return -EINVAL;
  228. watch = audit_init_watch(path);
  229. if (IS_ERR(watch))
  230. return PTR_ERR(watch);
  231. audit_get_watch(watch);
  232. krule->watch = watch;
  233. return 0;
  234. }
  235. static __u32 *classes[AUDIT_SYSCALL_CLASSES];
  236. int __init audit_register_class(int class, unsigned *list)
  237. {
  238. __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
  239. if (!p)
  240. return -ENOMEM;
  241. while (*list != ~0U) {
  242. unsigned n = *list++;
  243. if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
  244. kfree(p);
  245. return -EINVAL;
  246. }
  247. p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
  248. }
  249. if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
  250. kfree(p);
  251. return -EINVAL;
  252. }
  253. classes[class] = p;
  254. return 0;
  255. }
  256. int audit_match_class(int class, unsigned syscall)
  257. {
  258. if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
  259. return 0;
  260. if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
  261. return 0;
  262. return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
  263. }
  264. #ifdef CONFIG_AUDITSYSCALL
  265. static inline int audit_match_class_bits(int class, u32 *mask)
  266. {
  267. int i;
  268. if (classes[class]) {
  269. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  270. if (mask[i] & classes[class][i])
  271. return 0;
  272. }
  273. return 1;
  274. }
  275. static int audit_match_signal(struct audit_entry *entry)
  276. {
  277. struct audit_field *arch = entry->rule.arch_f;
  278. if (!arch) {
  279. /* When arch is unspecified, we must check both masks on biarch
  280. * as syscall number alone is ambiguous. */
  281. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  282. entry->rule.mask) &&
  283. audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  284. entry->rule.mask));
  285. }
  286. switch(audit_classify_arch(arch->val)) {
  287. case 0: /* native */
  288. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  289. entry->rule.mask));
  290. case 1: /* 32bit on biarch */
  291. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  292. entry->rule.mask));
  293. default:
  294. return 1;
  295. }
  296. }
  297. #endif
  298. /* Common user-space to kernel rule translation. */
  299. static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
  300. {
  301. unsigned listnr;
  302. struct audit_entry *entry;
  303. int i, err;
  304. err = -EINVAL;
  305. listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
  306. switch(listnr) {
  307. default:
  308. goto exit_err;
  309. case AUDIT_FILTER_USER:
  310. case AUDIT_FILTER_TYPE:
  311. #ifdef CONFIG_AUDITSYSCALL
  312. case AUDIT_FILTER_ENTRY:
  313. case AUDIT_FILTER_EXIT:
  314. case AUDIT_FILTER_TASK:
  315. #endif
  316. ;
  317. }
  318. if (unlikely(rule->action == AUDIT_POSSIBLE)) {
  319. printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
  320. goto exit_err;
  321. }
  322. if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
  323. goto exit_err;
  324. if (rule->field_count > AUDIT_MAX_FIELDS)
  325. goto exit_err;
  326. err = -ENOMEM;
  327. entry = audit_init_entry(rule->field_count);
  328. if (!entry)
  329. goto exit_err;
  330. entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
  331. entry->rule.listnr = listnr;
  332. entry->rule.action = rule->action;
  333. entry->rule.field_count = rule->field_count;
  334. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  335. entry->rule.mask[i] = rule->mask[i];
  336. for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
  337. int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
  338. __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
  339. __u32 *class;
  340. if (!(*p & AUDIT_BIT(bit)))
  341. continue;
  342. *p &= ~AUDIT_BIT(bit);
  343. class = classes[i];
  344. if (class) {
  345. int j;
  346. for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
  347. entry->rule.mask[j] |= class[j];
  348. }
  349. }
  350. return entry;
  351. exit_err:
  352. return ERR_PTR(err);
  353. }
  354. /* Translate struct audit_rule to kernel's rule respresentation.
  355. * Exists for backward compatibility with userspace. */
  356. static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
  357. {
  358. struct audit_entry *entry;
  359. struct audit_field *ino_f;
  360. int err = 0;
  361. int i;
  362. entry = audit_to_entry_common(rule);
  363. if (IS_ERR(entry))
  364. goto exit_nofree;
  365. for (i = 0; i < rule->field_count; i++) {
  366. struct audit_field *f = &entry->rule.fields[i];
  367. f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
  368. f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
  369. f->val = rule->values[i];
  370. err = -EINVAL;
  371. switch(f->type) {
  372. default:
  373. goto exit_free;
  374. case AUDIT_PID:
  375. case AUDIT_UID:
  376. case AUDIT_EUID:
  377. case AUDIT_SUID:
  378. case AUDIT_FSUID:
  379. case AUDIT_GID:
  380. case AUDIT_EGID:
  381. case AUDIT_SGID:
  382. case AUDIT_FSGID:
  383. case AUDIT_LOGINUID:
  384. case AUDIT_PERS:
  385. case AUDIT_MSGTYPE:
  386. case AUDIT_PPID:
  387. case AUDIT_DEVMAJOR:
  388. case AUDIT_DEVMINOR:
  389. case AUDIT_EXIT:
  390. case AUDIT_SUCCESS:
  391. /* bit ops are only useful on syscall args */
  392. if (f->op == AUDIT_BIT_MASK ||
  393. f->op == AUDIT_BIT_TEST) {
  394. err = -EINVAL;
  395. goto exit_free;
  396. }
  397. break;
  398. case AUDIT_ARG0:
  399. case AUDIT_ARG1:
  400. case AUDIT_ARG2:
  401. case AUDIT_ARG3:
  402. break;
  403. /* arch is only allowed to be = or != */
  404. case AUDIT_ARCH:
  405. if ((f->op != AUDIT_NOT_EQUAL) && (f->op != AUDIT_EQUAL)
  406. && (f->op != AUDIT_NEGATE) && (f->op)) {
  407. err = -EINVAL;
  408. goto exit_free;
  409. }
  410. entry->rule.arch_f = f;
  411. break;
  412. case AUDIT_PERM:
  413. if (f->val & ~15)
  414. goto exit_free;
  415. break;
  416. case AUDIT_FILETYPE:
  417. if ((f->val & ~S_IFMT) > S_IFMT)
  418. goto exit_free;
  419. break;
  420. case AUDIT_INODE:
  421. err = audit_to_inode(&entry->rule, f);
  422. if (err)
  423. goto exit_free;
  424. break;
  425. }
  426. entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
  427. /* Support for legacy operators where
  428. * AUDIT_NEGATE bit signifies != and otherwise assumes == */
  429. if (f->op & AUDIT_NEGATE)
  430. f->op = AUDIT_NOT_EQUAL;
  431. else if (!f->op)
  432. f->op = AUDIT_EQUAL;
  433. else if (f->op == AUDIT_OPERATORS) {
  434. err = -EINVAL;
  435. goto exit_free;
  436. }
  437. }
  438. ino_f = entry->rule.inode_f;
  439. if (ino_f) {
  440. switch(ino_f->op) {
  441. case AUDIT_NOT_EQUAL:
  442. entry->rule.inode_f = NULL;
  443. case AUDIT_EQUAL:
  444. break;
  445. default:
  446. err = -EINVAL;
  447. goto exit_free;
  448. }
  449. }
  450. exit_nofree:
  451. return entry;
  452. exit_free:
  453. audit_free_rule(entry);
  454. return ERR_PTR(err);
  455. }
  456. /* Translate struct audit_rule_data to kernel's rule respresentation. */
  457. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  458. size_t datasz)
  459. {
  460. int err = 0;
  461. struct audit_entry *entry;
  462. struct audit_field *ino_f;
  463. void *bufp;
  464. size_t remain = datasz - sizeof(struct audit_rule_data);
  465. int i;
  466. char *str;
  467. entry = audit_to_entry_common((struct audit_rule *)data);
  468. if (IS_ERR(entry))
  469. goto exit_nofree;
  470. bufp = data->buf;
  471. entry->rule.vers_ops = 2;
  472. for (i = 0; i < data->field_count; i++) {
  473. struct audit_field *f = &entry->rule.fields[i];
  474. err = -EINVAL;
  475. if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
  476. data->fieldflags[i] & ~AUDIT_OPERATORS)
  477. goto exit_free;
  478. f->op = data->fieldflags[i] & AUDIT_OPERATORS;
  479. f->type = data->fields[i];
  480. f->val = data->values[i];
  481. f->lsm_str = NULL;
  482. f->lsm_rule = NULL;
  483. switch(f->type) {
  484. case AUDIT_PID:
  485. case AUDIT_UID:
  486. case AUDIT_EUID:
  487. case AUDIT_SUID:
  488. case AUDIT_FSUID:
  489. case AUDIT_GID:
  490. case AUDIT_EGID:
  491. case AUDIT_SGID:
  492. case AUDIT_FSGID:
  493. case AUDIT_LOGINUID:
  494. case AUDIT_PERS:
  495. case AUDIT_MSGTYPE:
  496. case AUDIT_PPID:
  497. case AUDIT_DEVMAJOR:
  498. case AUDIT_DEVMINOR:
  499. case AUDIT_EXIT:
  500. case AUDIT_SUCCESS:
  501. case AUDIT_ARG0:
  502. case AUDIT_ARG1:
  503. case AUDIT_ARG2:
  504. case AUDIT_ARG3:
  505. break;
  506. case AUDIT_ARCH:
  507. entry->rule.arch_f = f;
  508. break;
  509. case AUDIT_SUBJ_USER:
  510. case AUDIT_SUBJ_ROLE:
  511. case AUDIT_SUBJ_TYPE:
  512. case AUDIT_SUBJ_SEN:
  513. case AUDIT_SUBJ_CLR:
  514. case AUDIT_OBJ_USER:
  515. case AUDIT_OBJ_ROLE:
  516. case AUDIT_OBJ_TYPE:
  517. case AUDIT_OBJ_LEV_LOW:
  518. case AUDIT_OBJ_LEV_HIGH:
  519. str = audit_unpack_string(&bufp, &remain, f->val);
  520. if (IS_ERR(str))
  521. goto exit_free;
  522. entry->rule.buflen += f->val;
  523. err = security_audit_rule_init(f->type, f->op, str,
  524. (void **)&f->lsm_rule);
  525. /* Keep currently invalid fields around in case they
  526. * become valid after a policy reload. */
  527. if (err == -EINVAL) {
  528. printk(KERN_WARNING "audit rule for LSM "
  529. "\'%s\' is invalid\n", str);
  530. err = 0;
  531. }
  532. if (err) {
  533. kfree(str);
  534. goto exit_free;
  535. } else
  536. f->lsm_str = str;
  537. break;
  538. case AUDIT_WATCH:
  539. str = audit_unpack_string(&bufp, &remain, f->val);
  540. if (IS_ERR(str))
  541. goto exit_free;
  542. entry->rule.buflen += f->val;
  543. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  544. if (err) {
  545. kfree(str);
  546. goto exit_free;
  547. }
  548. break;
  549. case AUDIT_DIR:
  550. str = audit_unpack_string(&bufp, &remain, f->val);
  551. if (IS_ERR(str))
  552. goto exit_free;
  553. entry->rule.buflen += f->val;
  554. err = audit_make_tree(&entry->rule, str, f->op);
  555. kfree(str);
  556. if (err)
  557. goto exit_free;
  558. break;
  559. case AUDIT_INODE:
  560. err = audit_to_inode(&entry->rule, f);
  561. if (err)
  562. goto exit_free;
  563. break;
  564. case AUDIT_FILTERKEY:
  565. err = -EINVAL;
  566. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  567. goto exit_free;
  568. str = audit_unpack_string(&bufp, &remain, f->val);
  569. if (IS_ERR(str))
  570. goto exit_free;
  571. entry->rule.buflen += f->val;
  572. entry->rule.filterkey = str;
  573. break;
  574. case AUDIT_PERM:
  575. if (f->val & ~15)
  576. goto exit_free;
  577. break;
  578. case AUDIT_FILETYPE:
  579. if ((f->val & ~S_IFMT) > S_IFMT)
  580. goto exit_free;
  581. break;
  582. default:
  583. goto exit_free;
  584. }
  585. }
  586. ino_f = entry->rule.inode_f;
  587. if (ino_f) {
  588. switch(ino_f->op) {
  589. case AUDIT_NOT_EQUAL:
  590. entry->rule.inode_f = NULL;
  591. case AUDIT_EQUAL:
  592. break;
  593. default:
  594. err = -EINVAL;
  595. goto exit_free;
  596. }
  597. }
  598. exit_nofree:
  599. return entry;
  600. exit_free:
  601. audit_free_rule(entry);
  602. return ERR_PTR(err);
  603. }
  604. /* Pack a filter field's string representation into data block. */
  605. static inline size_t audit_pack_string(void **bufp, const char *str)
  606. {
  607. size_t len = strlen(str);
  608. memcpy(*bufp, str, len);
  609. *bufp += len;
  610. return len;
  611. }
  612. /* Translate kernel rule respresentation to struct audit_rule.
  613. * Exists for backward compatibility with userspace. */
  614. static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
  615. {
  616. struct audit_rule *rule;
  617. int i;
  618. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  619. if (unlikely(!rule))
  620. return NULL;
  621. rule->flags = krule->flags | krule->listnr;
  622. rule->action = krule->action;
  623. rule->field_count = krule->field_count;
  624. for (i = 0; i < rule->field_count; i++) {
  625. rule->values[i] = krule->fields[i].val;
  626. rule->fields[i] = krule->fields[i].type;
  627. if (krule->vers_ops == 1) {
  628. if (krule->fields[i].op & AUDIT_NOT_EQUAL)
  629. rule->fields[i] |= AUDIT_NEGATE;
  630. } else {
  631. rule->fields[i] |= krule->fields[i].op;
  632. }
  633. }
  634. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
  635. return rule;
  636. }
  637. /* Translate kernel rule respresentation to struct audit_rule_data. */
  638. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  639. {
  640. struct audit_rule_data *data;
  641. void *bufp;
  642. int i;
  643. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  644. if (unlikely(!data))
  645. return NULL;
  646. memset(data, 0, sizeof(*data));
  647. data->flags = krule->flags | krule->listnr;
  648. data->action = krule->action;
  649. data->field_count = krule->field_count;
  650. bufp = data->buf;
  651. for (i = 0; i < data->field_count; i++) {
  652. struct audit_field *f = &krule->fields[i];
  653. data->fields[i] = f->type;
  654. data->fieldflags[i] = f->op;
  655. switch(f->type) {
  656. case AUDIT_SUBJ_USER:
  657. case AUDIT_SUBJ_ROLE:
  658. case AUDIT_SUBJ_TYPE:
  659. case AUDIT_SUBJ_SEN:
  660. case AUDIT_SUBJ_CLR:
  661. case AUDIT_OBJ_USER:
  662. case AUDIT_OBJ_ROLE:
  663. case AUDIT_OBJ_TYPE:
  664. case AUDIT_OBJ_LEV_LOW:
  665. case AUDIT_OBJ_LEV_HIGH:
  666. data->buflen += data->values[i] =
  667. audit_pack_string(&bufp, f->lsm_str);
  668. break;
  669. case AUDIT_WATCH:
  670. data->buflen += data->values[i] =
  671. audit_pack_string(&bufp, krule->watch->path);
  672. break;
  673. case AUDIT_DIR:
  674. data->buflen += data->values[i] =
  675. audit_pack_string(&bufp,
  676. audit_tree_path(krule->tree));
  677. break;
  678. case AUDIT_FILTERKEY:
  679. data->buflen += data->values[i] =
  680. audit_pack_string(&bufp, krule->filterkey);
  681. break;
  682. default:
  683. data->values[i] = f->val;
  684. }
  685. }
  686. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  687. return data;
  688. }
  689. /* Compare two rules in kernel format. Considered success if rules
  690. * don't match. */
  691. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  692. {
  693. int i;
  694. if (a->flags != b->flags ||
  695. a->listnr != b->listnr ||
  696. a->action != b->action ||
  697. a->field_count != b->field_count)
  698. return 1;
  699. for (i = 0; i < a->field_count; i++) {
  700. if (a->fields[i].type != b->fields[i].type ||
  701. a->fields[i].op != b->fields[i].op)
  702. return 1;
  703. switch(a->fields[i].type) {
  704. case AUDIT_SUBJ_USER:
  705. case AUDIT_SUBJ_ROLE:
  706. case AUDIT_SUBJ_TYPE:
  707. case AUDIT_SUBJ_SEN:
  708. case AUDIT_SUBJ_CLR:
  709. case AUDIT_OBJ_USER:
  710. case AUDIT_OBJ_ROLE:
  711. case AUDIT_OBJ_TYPE:
  712. case AUDIT_OBJ_LEV_LOW:
  713. case AUDIT_OBJ_LEV_HIGH:
  714. if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
  715. return 1;
  716. break;
  717. case AUDIT_WATCH:
  718. if (strcmp(a->watch->path, b->watch->path))
  719. return 1;
  720. break;
  721. case AUDIT_DIR:
  722. if (strcmp(audit_tree_path(a->tree),
  723. audit_tree_path(b->tree)))
  724. return 1;
  725. break;
  726. case AUDIT_FILTERKEY:
  727. /* both filterkeys exist based on above type compare */
  728. if (strcmp(a->filterkey, b->filterkey))
  729. return 1;
  730. break;
  731. default:
  732. if (a->fields[i].val != b->fields[i].val)
  733. return 1;
  734. }
  735. }
  736. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  737. if (a->mask[i] != b->mask[i])
  738. return 1;
  739. return 0;
  740. }
  741. /* Duplicate the given audit watch. The new watch's rules list is initialized
  742. * to an empty list and wlist is undefined. */
  743. static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
  744. {
  745. char *path;
  746. struct audit_watch *new;
  747. path = kstrdup(old->path, GFP_KERNEL);
  748. if (unlikely(!path))
  749. return ERR_PTR(-ENOMEM);
  750. new = audit_init_watch(path);
  751. if (IS_ERR(new)) {
  752. kfree(path);
  753. goto out;
  754. }
  755. new->dev = old->dev;
  756. new->ino = old->ino;
  757. get_inotify_watch(&old->parent->wdata);
  758. new->parent = old->parent;
  759. out:
  760. return new;
  761. }
  762. /* Duplicate LSM field information. The lsm_rule is opaque, so must be
  763. * re-initialized. */
  764. static inline int audit_dupe_lsm_field(struct audit_field *df,
  765. struct audit_field *sf)
  766. {
  767. int ret = 0;
  768. char *lsm_str;
  769. /* our own copy of lsm_str */
  770. lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
  771. if (unlikely(!lsm_str))
  772. return -ENOMEM;
  773. df->lsm_str = lsm_str;
  774. /* our own (refreshed) copy of lsm_rule */
  775. ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
  776. (void **)&df->lsm_rule);
  777. /* Keep currently invalid fields around in case they
  778. * become valid after a policy reload. */
  779. if (ret == -EINVAL) {
  780. printk(KERN_WARNING "audit rule for LSM \'%s\' is "
  781. "invalid\n", df->lsm_str);
  782. ret = 0;
  783. }
  784. return ret;
  785. }
  786. /* Duplicate an audit rule. This will be a deep copy with the exception
  787. * of the watch - that pointer is carried over. The LSM specific fields
  788. * will be updated in the copy. The point is to be able to replace the old
  789. * rule with the new rule in the filterlist, then free the old rule.
  790. * The rlist element is undefined; list manipulations are handled apart from
  791. * the initial copy. */
  792. static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
  793. struct audit_watch *watch)
  794. {
  795. u32 fcount = old->field_count;
  796. struct audit_entry *entry;
  797. struct audit_krule *new;
  798. char *fk;
  799. int i, err = 0;
  800. entry = audit_init_entry(fcount);
  801. if (unlikely(!entry))
  802. return ERR_PTR(-ENOMEM);
  803. new = &entry->rule;
  804. new->vers_ops = old->vers_ops;
  805. new->flags = old->flags;
  806. new->listnr = old->listnr;
  807. new->action = old->action;
  808. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  809. new->mask[i] = old->mask[i];
  810. new->buflen = old->buflen;
  811. new->inode_f = old->inode_f;
  812. new->watch = NULL;
  813. new->field_count = old->field_count;
  814. /*
  815. * note that we are OK with not refcounting here; audit_match_tree()
  816. * never dereferences tree and we can't get false positives there
  817. * since we'd have to have rule gone from the list *and* removed
  818. * before the chunks found by lookup had been allocated, i.e. before
  819. * the beginning of list scan.
  820. */
  821. new->tree = old->tree;
  822. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  823. /* deep copy this information, updating the lsm_rule fields, because
  824. * the originals will all be freed when the old rule is freed. */
  825. for (i = 0; i < fcount; i++) {
  826. switch (new->fields[i].type) {
  827. case AUDIT_SUBJ_USER:
  828. case AUDIT_SUBJ_ROLE:
  829. case AUDIT_SUBJ_TYPE:
  830. case AUDIT_SUBJ_SEN:
  831. case AUDIT_SUBJ_CLR:
  832. case AUDIT_OBJ_USER:
  833. case AUDIT_OBJ_ROLE:
  834. case AUDIT_OBJ_TYPE:
  835. case AUDIT_OBJ_LEV_LOW:
  836. case AUDIT_OBJ_LEV_HIGH:
  837. err = audit_dupe_lsm_field(&new->fields[i],
  838. &old->fields[i]);
  839. break;
  840. case AUDIT_FILTERKEY:
  841. fk = kstrdup(old->filterkey, GFP_KERNEL);
  842. if (unlikely(!fk))
  843. err = -ENOMEM;
  844. else
  845. new->filterkey = fk;
  846. }
  847. if (err) {
  848. audit_free_rule(entry);
  849. return ERR_PTR(err);
  850. }
  851. }
  852. if (watch) {
  853. audit_get_watch(watch);
  854. new->watch = watch;
  855. }
  856. return entry;
  857. }
  858. /* Update inode info in audit rules based on filesystem event. */
  859. static void audit_update_watch(struct audit_parent *parent,
  860. const char *dname, dev_t dev,
  861. unsigned long ino, unsigned invalidating)
  862. {
  863. struct audit_watch *owatch, *nwatch, *nextw;
  864. struct audit_krule *r, *nextr;
  865. struct audit_entry *oentry, *nentry;
  866. mutex_lock(&audit_filter_mutex);
  867. list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
  868. if (audit_compare_dname_path(dname, owatch->path, NULL))
  869. continue;
  870. /* If the update involves invalidating rules, do the inode-based
  871. * filtering now, so we don't omit records. */
  872. if (invalidating && current->audit_context &&
  873. audit_filter_inodes(current, current->audit_context) == AUDIT_RECORD_CONTEXT)
  874. audit_set_auditable(current->audit_context);
  875. nwatch = audit_dupe_watch(owatch);
  876. if (IS_ERR(nwatch)) {
  877. mutex_unlock(&audit_filter_mutex);
  878. audit_panic("error updating watch, skipping");
  879. return;
  880. }
  881. nwatch->dev = dev;
  882. nwatch->ino = ino;
  883. list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
  884. oentry = container_of(r, struct audit_entry, rule);
  885. list_del(&oentry->rule.rlist);
  886. list_del_rcu(&oentry->list);
  887. nentry = audit_dupe_rule(&oentry->rule, nwatch);
  888. if (IS_ERR(nentry))
  889. audit_panic("error updating watch, removing");
  890. else {
  891. int h = audit_hash_ino((u32)ino);
  892. list_add(&nentry->rule.rlist, &nwatch->rules);
  893. list_add_rcu(&nentry->list, &audit_inode_hash[h]);
  894. }
  895. call_rcu(&oentry->rcu, audit_free_rule_rcu);
  896. }
  897. if (audit_enabled) {
  898. struct audit_buffer *ab;
  899. ab = audit_log_start(NULL, GFP_KERNEL,
  900. AUDIT_CONFIG_CHANGE);
  901. audit_log_format(ab,
  902. "op=updated rules specifying path=");
  903. audit_log_untrustedstring(ab, owatch->path);
  904. audit_log_format(ab, " with dev=%u ino=%lu\n",
  905. dev, ino);
  906. audit_log_format(ab, " list=%d res=1", r->listnr);
  907. audit_log_end(ab);
  908. }
  909. audit_remove_watch(owatch);
  910. goto add_watch_to_parent; /* event applies to a single watch */
  911. }
  912. mutex_unlock(&audit_filter_mutex);
  913. return;
  914. add_watch_to_parent:
  915. list_add(&nwatch->wlist, &parent->watches);
  916. mutex_unlock(&audit_filter_mutex);
  917. return;
  918. }
  919. /* Remove all watches & rules associated with a parent that is going away. */
  920. static void audit_remove_parent_watches(struct audit_parent *parent)
  921. {
  922. struct audit_watch *w, *nextw;
  923. struct audit_krule *r, *nextr;
  924. struct audit_entry *e;
  925. mutex_lock(&audit_filter_mutex);
  926. parent->flags |= AUDIT_PARENT_INVALID;
  927. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  928. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  929. e = container_of(r, struct audit_entry, rule);
  930. if (audit_enabled) {
  931. struct audit_buffer *ab;
  932. ab = audit_log_start(NULL, GFP_KERNEL,
  933. AUDIT_CONFIG_CHANGE);
  934. audit_log_format(ab, "op=remove rule path=");
  935. audit_log_untrustedstring(ab, w->path);
  936. if (r->filterkey) {
  937. audit_log_format(ab, " key=");
  938. audit_log_untrustedstring(ab,
  939. r->filterkey);
  940. } else
  941. audit_log_format(ab, " key=(null)");
  942. audit_log_format(ab, " list=%d res=1",
  943. r->listnr);
  944. audit_log_end(ab);
  945. }
  946. list_del(&r->rlist);
  947. list_del_rcu(&e->list);
  948. call_rcu(&e->rcu, audit_free_rule_rcu);
  949. }
  950. audit_remove_watch(w);
  951. }
  952. mutex_unlock(&audit_filter_mutex);
  953. }
  954. /* Unregister inotify watches for parents on in_list.
  955. * Generates an IN_IGNORED event. */
  956. static void audit_inotify_unregister(struct list_head *in_list)
  957. {
  958. struct audit_parent *p, *n;
  959. list_for_each_entry_safe(p, n, in_list, ilist) {
  960. list_del(&p->ilist);
  961. inotify_rm_watch(audit_ih, &p->wdata);
  962. /* the put matching the get in audit_do_del_rule() */
  963. put_inotify_watch(&p->wdata);
  964. }
  965. }
  966. /* Find an existing audit rule.
  967. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  968. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  969. struct list_head *list)
  970. {
  971. struct audit_entry *e, *found = NULL;
  972. int h;
  973. if (entry->rule.watch) {
  974. /* we don't know the inode number, so must walk entire hash */
  975. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  976. list = &audit_inode_hash[h];
  977. list_for_each_entry(e, list, list)
  978. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  979. found = e;
  980. goto out;
  981. }
  982. }
  983. goto out;
  984. }
  985. list_for_each_entry(e, list, list)
  986. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  987. found = e;
  988. goto out;
  989. }
  990. out:
  991. return found;
  992. }
  993. /* Get path information necessary for adding watches. */
  994. static int audit_get_nd(char *path, struct nameidata **ndp,
  995. struct nameidata **ndw)
  996. {
  997. struct nameidata *ndparent, *ndwatch;
  998. int err;
  999. ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
  1000. if (unlikely(!ndparent))
  1001. return -ENOMEM;
  1002. ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
  1003. if (unlikely(!ndwatch)) {
  1004. kfree(ndparent);
  1005. return -ENOMEM;
  1006. }
  1007. err = path_lookup(path, LOOKUP_PARENT, ndparent);
  1008. if (err) {
  1009. kfree(ndparent);
  1010. kfree(ndwatch);
  1011. return err;
  1012. }
  1013. err = path_lookup(path, 0, ndwatch);
  1014. if (err) {
  1015. kfree(ndwatch);
  1016. ndwatch = NULL;
  1017. }
  1018. *ndp = ndparent;
  1019. *ndw = ndwatch;
  1020. return 0;
  1021. }
  1022. /* Release resources used for watch path information. */
  1023. static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
  1024. {
  1025. if (ndp) {
  1026. path_put(&ndp->path);
  1027. kfree(ndp);
  1028. }
  1029. if (ndw) {
  1030. path_put(&ndw->path);
  1031. kfree(ndw);
  1032. }
  1033. }
  1034. /* Associate the given rule with an existing parent inotify_watch.
  1035. * Caller must hold audit_filter_mutex. */
  1036. static void audit_add_to_parent(struct audit_krule *krule,
  1037. struct audit_parent *parent)
  1038. {
  1039. struct audit_watch *w, *watch = krule->watch;
  1040. int watch_found = 0;
  1041. list_for_each_entry(w, &parent->watches, wlist) {
  1042. if (strcmp(watch->path, w->path))
  1043. continue;
  1044. watch_found = 1;
  1045. /* put krule's and initial refs to temporary watch */
  1046. audit_put_watch(watch);
  1047. audit_put_watch(watch);
  1048. audit_get_watch(w);
  1049. krule->watch = watch = w;
  1050. break;
  1051. }
  1052. if (!watch_found) {
  1053. get_inotify_watch(&parent->wdata);
  1054. watch->parent = parent;
  1055. list_add(&watch->wlist, &parent->watches);
  1056. }
  1057. list_add(&krule->rlist, &watch->rules);
  1058. }
  1059. /* Find a matching watch entry, or add this one.
  1060. * Caller must hold audit_filter_mutex. */
  1061. static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
  1062. struct nameidata *ndw)
  1063. {
  1064. struct audit_watch *watch = krule->watch;
  1065. struct inotify_watch *i_watch;
  1066. struct audit_parent *parent;
  1067. int ret = 0;
  1068. /* update watch filter fields */
  1069. if (ndw) {
  1070. watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
  1071. watch->ino = ndw->path.dentry->d_inode->i_ino;
  1072. }
  1073. /* The audit_filter_mutex must not be held during inotify calls because
  1074. * we hold it during inotify event callback processing. If an existing
  1075. * inotify watch is found, inotify_find_watch() grabs a reference before
  1076. * returning.
  1077. */
  1078. mutex_unlock(&audit_filter_mutex);
  1079. if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
  1080. &i_watch) < 0) {
  1081. parent = audit_init_parent(ndp);
  1082. if (IS_ERR(parent)) {
  1083. /* caller expects mutex locked */
  1084. mutex_lock(&audit_filter_mutex);
  1085. return PTR_ERR(parent);
  1086. }
  1087. } else
  1088. parent = container_of(i_watch, struct audit_parent, wdata);
  1089. mutex_lock(&audit_filter_mutex);
  1090. /* parent was moved before we took audit_filter_mutex */
  1091. if (parent->flags & AUDIT_PARENT_INVALID)
  1092. ret = -ENOENT;
  1093. else
  1094. audit_add_to_parent(krule, parent);
  1095. /* match get in audit_init_parent or inotify_find_watch */
  1096. put_inotify_watch(&parent->wdata);
  1097. return ret;
  1098. }
  1099. /* Add rule to given filterlist if not a duplicate. */
  1100. static inline int audit_add_rule(struct audit_entry *entry,
  1101. struct list_head *list)
  1102. {
  1103. struct audit_entry *e;
  1104. struct audit_field *inode_f = entry->rule.inode_f;
  1105. struct audit_watch *watch = entry->rule.watch;
  1106. struct audit_tree *tree = entry->rule.tree;
  1107. struct nameidata *ndp = NULL, *ndw = NULL;
  1108. int h, err;
  1109. #ifdef CONFIG_AUDITSYSCALL
  1110. int dont_count = 0;
  1111. /* If either of these, don't count towards total */
  1112. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  1113. entry->rule.listnr == AUDIT_FILTER_TYPE)
  1114. dont_count = 1;
  1115. #endif
  1116. if (inode_f) {
  1117. h = audit_hash_ino(inode_f->val);
  1118. list = &audit_inode_hash[h];
  1119. }
  1120. mutex_lock(&audit_filter_mutex);
  1121. e = audit_find_rule(entry, list);
  1122. mutex_unlock(&audit_filter_mutex);
  1123. if (e) {
  1124. err = -EEXIST;
  1125. /* normally audit_add_tree_rule() will free it on failure */
  1126. if (tree)
  1127. audit_put_tree(tree);
  1128. goto error;
  1129. }
  1130. /* Avoid calling path_lookup under audit_filter_mutex. */
  1131. if (watch) {
  1132. err = audit_get_nd(watch->path, &ndp, &ndw);
  1133. if (err)
  1134. goto error;
  1135. }
  1136. mutex_lock(&audit_filter_mutex);
  1137. if (watch) {
  1138. /* audit_filter_mutex is dropped and re-taken during this call */
  1139. err = audit_add_watch(&entry->rule, ndp, ndw);
  1140. if (err) {
  1141. mutex_unlock(&audit_filter_mutex);
  1142. goto error;
  1143. }
  1144. h = audit_hash_ino((u32)watch->ino);
  1145. list = &audit_inode_hash[h];
  1146. }
  1147. if (tree) {
  1148. err = audit_add_tree_rule(&entry->rule);
  1149. if (err) {
  1150. mutex_unlock(&audit_filter_mutex);
  1151. goto error;
  1152. }
  1153. }
  1154. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  1155. list_add_rcu(&entry->list, list);
  1156. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  1157. } else {
  1158. list_add_tail_rcu(&entry->list, list);
  1159. }
  1160. #ifdef CONFIG_AUDITSYSCALL
  1161. if (!dont_count)
  1162. audit_n_rules++;
  1163. if (!audit_match_signal(entry))
  1164. audit_signals++;
  1165. #endif
  1166. mutex_unlock(&audit_filter_mutex);
  1167. audit_put_nd(ndp, ndw); /* NULL args OK */
  1168. return 0;
  1169. error:
  1170. audit_put_nd(ndp, ndw); /* NULL args OK */
  1171. if (watch)
  1172. audit_put_watch(watch); /* tmp watch, matches initial get */
  1173. return err;
  1174. }
  1175. /* Remove an existing rule from filterlist. */
  1176. static inline int audit_del_rule(struct audit_entry *entry,
  1177. struct list_head *list)
  1178. {
  1179. struct audit_entry *e;
  1180. struct audit_field *inode_f = entry->rule.inode_f;
  1181. struct audit_watch *watch, *tmp_watch = entry->rule.watch;
  1182. struct audit_tree *tree = entry->rule.tree;
  1183. LIST_HEAD(inotify_list);
  1184. int h, ret = 0;
  1185. #ifdef CONFIG_AUDITSYSCALL
  1186. int dont_count = 0;
  1187. /* If either of these, don't count towards total */
  1188. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  1189. entry->rule.listnr == AUDIT_FILTER_TYPE)
  1190. dont_count = 1;
  1191. #endif
  1192. if (inode_f) {
  1193. h = audit_hash_ino(inode_f->val);
  1194. list = &audit_inode_hash[h];
  1195. }
  1196. mutex_lock(&audit_filter_mutex);
  1197. e = audit_find_rule(entry, list);
  1198. if (!e) {
  1199. mutex_unlock(&audit_filter_mutex);
  1200. ret = -ENOENT;
  1201. goto out;
  1202. }
  1203. watch = e->rule.watch;
  1204. if (watch) {
  1205. struct audit_parent *parent = watch->parent;
  1206. list_del(&e->rule.rlist);
  1207. if (list_empty(&watch->rules)) {
  1208. audit_remove_watch(watch);
  1209. if (list_empty(&parent->watches)) {
  1210. /* Put parent on the inotify un-registration
  1211. * list. Grab a reference before releasing
  1212. * audit_filter_mutex, to be released in
  1213. * audit_inotify_unregister(). */
  1214. list_add(&parent->ilist, &inotify_list);
  1215. get_inotify_watch(&parent->wdata);
  1216. }
  1217. }
  1218. }
  1219. if (e->rule.tree)
  1220. audit_remove_tree_rule(&e->rule);
  1221. list_del_rcu(&e->list);
  1222. call_rcu(&e->rcu, audit_free_rule_rcu);
  1223. #ifdef CONFIG_AUDITSYSCALL
  1224. if (!dont_count)
  1225. audit_n_rules--;
  1226. if (!audit_match_signal(entry))
  1227. audit_signals--;
  1228. #endif
  1229. mutex_unlock(&audit_filter_mutex);
  1230. if (!list_empty(&inotify_list))
  1231. audit_inotify_unregister(&inotify_list);
  1232. out:
  1233. if (tmp_watch)
  1234. audit_put_watch(tmp_watch); /* match initial get */
  1235. if (tree)
  1236. audit_put_tree(tree); /* that's the temporary one */
  1237. return ret;
  1238. }
  1239. /* List rules using struct audit_rule. Exists for backward
  1240. * compatibility with userspace. */
  1241. static void audit_list(int pid, int seq, struct sk_buff_head *q)
  1242. {
  1243. struct sk_buff *skb;
  1244. struct audit_entry *entry;
  1245. int i;
  1246. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  1247. * iterator to sync with list writers. */
  1248. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  1249. list_for_each_entry(entry, &audit_filter_list[i], list) {
  1250. struct audit_rule *rule;
  1251. rule = audit_krule_to_rule(&entry->rule);
  1252. if (unlikely(!rule))
  1253. break;
  1254. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  1255. rule, sizeof(*rule));
  1256. if (skb)
  1257. skb_queue_tail(q, skb);
  1258. kfree(rule);
  1259. }
  1260. }
  1261. for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
  1262. list_for_each_entry(entry, &audit_inode_hash[i], list) {
  1263. struct audit_rule *rule;
  1264. rule = audit_krule_to_rule(&entry->rule);
  1265. if (unlikely(!rule))
  1266. break;
  1267. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  1268. rule, sizeof(*rule));
  1269. if (skb)
  1270. skb_queue_tail(q, skb);
  1271. kfree(rule);
  1272. }
  1273. }
  1274. skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  1275. if (skb)
  1276. skb_queue_tail(q, skb);
  1277. }
  1278. /* List rules using struct audit_rule_data. */
  1279. static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
  1280. {
  1281. struct sk_buff *skb;
  1282. struct audit_entry *e;
  1283. int i;
  1284. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  1285. * iterator to sync with list writers. */
  1286. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  1287. list_for_each_entry(e, &audit_filter_list[i], list) {
  1288. struct audit_rule_data *data;
  1289. data = audit_krule_to_data(&e->rule);
  1290. if (unlikely(!data))
  1291. break;
  1292. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  1293. data, sizeof(*data) + data->buflen);
  1294. if (skb)
  1295. skb_queue_tail(q, skb);
  1296. kfree(data);
  1297. }
  1298. }
  1299. for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
  1300. list_for_each_entry(e, &audit_inode_hash[i], list) {
  1301. struct audit_rule_data *data;
  1302. data = audit_krule_to_data(&e->rule);
  1303. if (unlikely(!data))
  1304. break;
  1305. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  1306. data, sizeof(*data) + data->buflen);
  1307. if (skb)
  1308. skb_queue_tail(q, skb);
  1309. kfree(data);
  1310. }
  1311. }
  1312. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  1313. if (skb)
  1314. skb_queue_tail(q, skb);
  1315. }
  1316. /* Log rule additions and removals */
  1317. static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
  1318. char *action, struct audit_krule *rule,
  1319. int res)
  1320. {
  1321. struct audit_buffer *ab;
  1322. if (!audit_enabled)
  1323. return;
  1324. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  1325. if (!ab)
  1326. return;
  1327. audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
  1328. if (sid) {
  1329. char *ctx = NULL;
  1330. u32 len;
  1331. if (security_secid_to_secctx(sid, &ctx, &len))
  1332. audit_log_format(ab, " ssid=%u", sid);
  1333. else {
  1334. audit_log_format(ab, " subj=%s", ctx);
  1335. security_release_secctx(ctx, len);
  1336. }
  1337. }
  1338. audit_log_format(ab, " op=%s rule key=", action);
  1339. if (rule->filterkey)
  1340. audit_log_untrustedstring(ab, rule->filterkey);
  1341. else
  1342. audit_log_format(ab, "(null)");
  1343. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  1344. audit_log_end(ab);
  1345. }
  1346. /**
  1347. * audit_receive_filter - apply all rules to the specified message type
  1348. * @type: audit message type
  1349. * @pid: target pid for netlink audit messages
  1350. * @uid: target uid for netlink audit messages
  1351. * @seq: netlink audit message sequence (serial) number
  1352. * @data: payload data
  1353. * @datasz: size of payload data
  1354. * @loginuid: loginuid of sender
  1355. * @sid: SE Linux Security ID of sender
  1356. */
  1357. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  1358. size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
  1359. {
  1360. struct task_struct *tsk;
  1361. struct audit_netlink_list *dest;
  1362. int err = 0;
  1363. struct audit_entry *entry;
  1364. switch (type) {
  1365. case AUDIT_LIST:
  1366. case AUDIT_LIST_RULES:
  1367. /* We can't just spew out the rules here because we might fill
  1368. * the available socket buffer space and deadlock waiting for
  1369. * auditctl to read from it... which isn't ever going to
  1370. * happen if we're actually running in the context of auditctl
  1371. * trying to _send_ the stuff */
  1372. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1373. if (!dest)
  1374. return -ENOMEM;
  1375. dest->pid = pid;
  1376. skb_queue_head_init(&dest->q);
  1377. mutex_lock(&audit_filter_mutex);
  1378. if (type == AUDIT_LIST)
  1379. audit_list(pid, seq, &dest->q);
  1380. else
  1381. audit_list_rules(pid, seq, &dest->q);
  1382. mutex_unlock(&audit_filter_mutex);
  1383. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1384. if (IS_ERR(tsk)) {
  1385. skb_queue_purge(&dest->q);
  1386. kfree(dest);
  1387. err = PTR_ERR(tsk);
  1388. }
  1389. break;
  1390. case AUDIT_ADD:
  1391. case AUDIT_ADD_RULE:
  1392. if (type == AUDIT_ADD)
  1393. entry = audit_rule_to_entry(data);
  1394. else
  1395. entry = audit_data_to_entry(data, datasz);
  1396. if (IS_ERR(entry))
  1397. return PTR_ERR(entry);
  1398. err = audit_add_rule(entry,
  1399. &audit_filter_list[entry->rule.listnr]);
  1400. audit_log_rule_change(loginuid, sessionid, sid, "add",
  1401. &entry->rule, !err);
  1402. if (err)
  1403. audit_free_rule(entry);
  1404. break;
  1405. case AUDIT_DEL:
  1406. case AUDIT_DEL_RULE:
  1407. if (type == AUDIT_DEL)
  1408. entry = audit_rule_to_entry(data);
  1409. else
  1410. entry = audit_data_to_entry(data, datasz);
  1411. if (IS_ERR(entry))
  1412. return PTR_ERR(entry);
  1413. err = audit_del_rule(entry,
  1414. &audit_filter_list[entry->rule.listnr]);
  1415. audit_log_rule_change(loginuid, sessionid, sid, "remove",
  1416. &entry->rule, !err);
  1417. audit_free_rule(entry);
  1418. break;
  1419. default:
  1420. return -EINVAL;
  1421. }
  1422. return err;
  1423. }
  1424. int audit_comparator(const u32 left, const u32 op, const u32 right)
  1425. {
  1426. switch (op) {
  1427. case AUDIT_EQUAL:
  1428. return (left == right);
  1429. case AUDIT_NOT_EQUAL:
  1430. return (left != right);
  1431. case AUDIT_LESS_THAN:
  1432. return (left < right);
  1433. case AUDIT_LESS_THAN_OR_EQUAL:
  1434. return (left <= right);
  1435. case AUDIT_GREATER_THAN:
  1436. return (left > right);
  1437. case AUDIT_GREATER_THAN_OR_EQUAL:
  1438. return (left >= right);
  1439. case AUDIT_BIT_MASK:
  1440. return (left & right);
  1441. case AUDIT_BIT_TEST:
  1442. return ((left & right) == right);
  1443. }
  1444. BUG();
  1445. return 0;
  1446. }
  1447. /* Compare given dentry name with last component in given path,
  1448. * return of 0 indicates a match. */
  1449. int audit_compare_dname_path(const char *dname, const char *path,
  1450. int *dirlen)
  1451. {
  1452. int dlen, plen;
  1453. const char *p;
  1454. if (!dname || !path)
  1455. return 1;
  1456. dlen = strlen(dname);
  1457. plen = strlen(path);
  1458. if (plen < dlen)
  1459. return 1;
  1460. /* disregard trailing slashes */
  1461. p = path + plen - 1;
  1462. while ((*p == '/') && (p > path))
  1463. p--;
  1464. /* find last path component */
  1465. p = p - dlen + 1;
  1466. if (p < path)
  1467. return 1;
  1468. else if (p > path) {
  1469. if (*--p != '/')
  1470. return 1;
  1471. else
  1472. p++;
  1473. }
  1474. /* return length of path's directory component */
  1475. if (dirlen)
  1476. *dirlen = p - path;
  1477. return strncmp(p, dname, dlen);
  1478. }
  1479. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  1480. struct audit_krule *rule,
  1481. enum audit_state *state)
  1482. {
  1483. int i;
  1484. for (i = 0; i < rule->field_count; i++) {
  1485. struct audit_field *f = &rule->fields[i];
  1486. int result = 0;
  1487. switch (f->type) {
  1488. case AUDIT_PID:
  1489. result = audit_comparator(cb->creds.pid, f->op, f->val);
  1490. break;
  1491. case AUDIT_UID:
  1492. result = audit_comparator(cb->creds.uid, f->op, f->val);
  1493. break;
  1494. case AUDIT_GID:
  1495. result = audit_comparator(cb->creds.gid, f->op, f->val);
  1496. break;
  1497. case AUDIT_LOGINUID:
  1498. result = audit_comparator(cb->loginuid, f->op, f->val);
  1499. break;
  1500. }
  1501. if (!result)
  1502. return 0;
  1503. }
  1504. switch (rule->action) {
  1505. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  1506. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  1507. }
  1508. return 1;
  1509. }
  1510. int audit_filter_user(struct netlink_skb_parms *cb, int type)
  1511. {
  1512. enum audit_state state = AUDIT_DISABLED;
  1513. struct audit_entry *e;
  1514. int ret = 1;
  1515. rcu_read_lock();
  1516. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  1517. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  1518. if (state == AUDIT_DISABLED)
  1519. ret = 0;
  1520. break;
  1521. }
  1522. }
  1523. rcu_read_unlock();
  1524. return ret; /* Audit by default */
  1525. }
  1526. int audit_filter_type(int type)
  1527. {
  1528. struct audit_entry *e;
  1529. int result = 0;
  1530. rcu_read_lock();
  1531. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  1532. goto unlock_and_return;
  1533. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  1534. list) {
  1535. int i;
  1536. for (i = 0; i < e->rule.field_count; i++) {
  1537. struct audit_field *f = &e->rule.fields[i];
  1538. if (f->type == AUDIT_MSGTYPE) {
  1539. result = audit_comparator(type, f->op, f->val);
  1540. if (!result)
  1541. break;
  1542. }
  1543. }
  1544. if (result)
  1545. goto unlock_and_return;
  1546. }
  1547. unlock_and_return:
  1548. rcu_read_unlock();
  1549. return result;
  1550. }
  1551. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1552. * It will traverse the filter lists serarching for rules that contain LSM
  1553. * specific filter fields. When such a rule is found, it is copied, the
  1554. * LSM field is re-initialized, and the old rule is replaced with the
  1555. * updated rule. */
  1556. int audit_update_lsm_rules(void)
  1557. {
  1558. struct audit_entry *entry, *n, *nentry;
  1559. struct audit_watch *watch;
  1560. struct audit_tree *tree;
  1561. int i, err = 0;
  1562. /* audit_filter_mutex synchronizes the writers */
  1563. mutex_lock(&audit_filter_mutex);
  1564. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1565. list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
  1566. if (!security_audit_rule_known(&entry->rule))
  1567. continue;
  1568. watch = entry->rule.watch;
  1569. tree = entry->rule.tree;
  1570. nentry = audit_dupe_rule(&entry->rule, watch);
  1571. if (IS_ERR(nentry)) {
  1572. /* save the first error encountered for the
  1573. * return value */
  1574. if (!err)
  1575. err = PTR_ERR(nentry);
  1576. audit_panic("error updating LSM filters");
  1577. if (watch)
  1578. list_del(&entry->rule.rlist);
  1579. list_del_rcu(&entry->list);
  1580. } else {
  1581. if (watch) {
  1582. list_add(&nentry->rule.rlist,
  1583. &watch->rules);
  1584. list_del(&entry->rule.rlist);
  1585. } else if (tree)
  1586. list_replace_init(&entry->rule.rlist,
  1587. &nentry->rule.rlist);
  1588. list_replace_rcu(&entry->list, &nentry->list);
  1589. }
  1590. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1591. }
  1592. }
  1593. mutex_unlock(&audit_filter_mutex);
  1594. return err;
  1595. }
  1596. /* Update watch data in audit rules based on inotify events. */
  1597. void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
  1598. u32 cookie, const char *dname, struct inode *inode)
  1599. {
  1600. struct audit_parent *parent;
  1601. parent = container_of(i_watch, struct audit_parent, wdata);
  1602. if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
  1603. audit_update_watch(parent, dname, inode->i_sb->s_dev,
  1604. inode->i_ino, 0);
  1605. else if (mask & (IN_DELETE|IN_MOVED_FROM))
  1606. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  1607. /* inotify automatically removes the watch and sends IN_IGNORED */
  1608. else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
  1609. audit_remove_parent_watches(parent);
  1610. /* inotify does not remove the watch, so remove it manually */
  1611. else if(mask & IN_MOVE_SELF) {
  1612. audit_remove_parent_watches(parent);
  1613. inotify_remove_watch_locked(audit_ih, i_watch);
  1614. } else if (mask & IN_IGNORED)
  1615. put_inotify_watch(i_watch);
  1616. }