auditfilter.c 46 KB

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