auditfilter.c 39 KB

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