auditfilter.c 46 KB

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