auditfilter.c 41 KB

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