auditfilter.c 43 KB

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