auditfilter.c 46 KB

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