auditfilter.c 44 KB

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