auditfilter.c 42 KB

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