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