auditfilter.c 45 KB

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