auditfilter.c 43 KB

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