auditfilter.c 40 KB

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