auditfilter.c 46 KB

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