auditfilter.c 46 KB

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