auditfilter.c 44 KB

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