auditfilter.c 42 KB

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