auditfilter.c 44 KB

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