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/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, "auid=%u ses=%u",
  902. audit_get_loginuid(current),
  903. audit_get_sessionid(current));
  904. audit_log_format(ab,
  905. " op=updated rules specifying path=");
  906. audit_log_untrustedstring(ab, owatch->path);
  907. audit_log_format(ab, " with dev=%u ino=%lu\n",
  908. dev, ino);
  909. audit_log_format(ab, " list=%d res=1", r->listnr);
  910. audit_log_end(ab);
  911. }
  912. audit_remove_watch(owatch);
  913. goto add_watch_to_parent; /* event applies to a single watch */
  914. }
  915. mutex_unlock(&audit_filter_mutex);
  916. return;
  917. add_watch_to_parent:
  918. list_add(&nwatch->wlist, &parent->watches);
  919. mutex_unlock(&audit_filter_mutex);
  920. return;
  921. }
  922. /* Remove all watches & rules associated with a parent that is going away. */
  923. static void audit_remove_parent_watches(struct audit_parent *parent)
  924. {
  925. struct audit_watch *w, *nextw;
  926. struct audit_krule *r, *nextr;
  927. struct audit_entry *e;
  928. mutex_lock(&audit_filter_mutex);
  929. parent->flags |= AUDIT_PARENT_INVALID;
  930. list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
  931. list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
  932. e = container_of(r, struct audit_entry, rule);
  933. if (audit_enabled) {
  934. struct audit_buffer *ab;
  935. ab = audit_log_start(NULL, GFP_KERNEL,
  936. AUDIT_CONFIG_CHANGE);
  937. audit_log_format(ab, "auid=%u ses=%u",
  938. audit_get_loginuid(current),
  939. audit_get_sessionid(current));
  940. audit_log_format(ab, " op=remove rule path=");
  941. audit_log_untrustedstring(ab, w->path);
  942. if (r->filterkey) {
  943. audit_log_format(ab, " key=");
  944. audit_log_untrustedstring(ab,
  945. r->filterkey);
  946. } else
  947. audit_log_format(ab, " key=(null)");
  948. audit_log_format(ab, " list=%d res=1",
  949. r->listnr);
  950. audit_log_end(ab);
  951. }
  952. list_del(&r->rlist);
  953. list_del_rcu(&e->list);
  954. call_rcu(&e->rcu, audit_free_rule_rcu);
  955. }
  956. audit_remove_watch(w);
  957. }
  958. mutex_unlock(&audit_filter_mutex);
  959. }
  960. /* Unregister inotify watches for parents on in_list.
  961. * Generates an IN_IGNORED event. */
  962. static void audit_inotify_unregister(struct list_head *in_list)
  963. {
  964. struct audit_parent *p, *n;
  965. list_for_each_entry_safe(p, n, in_list, ilist) {
  966. list_del(&p->ilist);
  967. inotify_rm_watch(audit_ih, &p->wdata);
  968. /* the put matching the get in audit_do_del_rule() */
  969. put_inotify_watch(&p->wdata);
  970. }
  971. }
  972. /* Find an existing audit rule.
  973. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  974. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  975. struct list_head *list)
  976. {
  977. struct audit_entry *e, *found = NULL;
  978. int h;
  979. if (entry->rule.watch) {
  980. /* we don't know the inode number, so must walk entire hash */
  981. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  982. list = &audit_inode_hash[h];
  983. list_for_each_entry(e, list, list)
  984. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  985. found = e;
  986. goto out;
  987. }
  988. }
  989. goto out;
  990. }
  991. list_for_each_entry(e, list, list)
  992. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  993. found = e;
  994. goto out;
  995. }
  996. out:
  997. return found;
  998. }
  999. /* Get path information necessary for adding watches. */
  1000. static int audit_get_nd(char *path, struct nameidata **ndp,
  1001. struct nameidata **ndw)
  1002. {
  1003. struct nameidata *ndparent, *ndwatch;
  1004. int err;
  1005. ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
  1006. if (unlikely(!ndparent))
  1007. return -ENOMEM;
  1008. ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
  1009. if (unlikely(!ndwatch)) {
  1010. kfree(ndparent);
  1011. return -ENOMEM;
  1012. }
  1013. err = path_lookup(path, LOOKUP_PARENT, ndparent);
  1014. if (err) {
  1015. kfree(ndparent);
  1016. kfree(ndwatch);
  1017. return err;
  1018. }
  1019. err = path_lookup(path, 0, ndwatch);
  1020. if (err) {
  1021. kfree(ndwatch);
  1022. ndwatch = NULL;
  1023. }
  1024. *ndp = ndparent;
  1025. *ndw = ndwatch;
  1026. return 0;
  1027. }
  1028. /* Release resources used for watch path information. */
  1029. static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
  1030. {
  1031. if (ndp) {
  1032. path_put(&ndp->path);
  1033. kfree(ndp);
  1034. }
  1035. if (ndw) {
  1036. path_put(&ndw->path);
  1037. kfree(ndw);
  1038. }
  1039. }
  1040. /* Associate the given rule with an existing parent inotify_watch.
  1041. * Caller must hold audit_filter_mutex. */
  1042. static void audit_add_to_parent(struct audit_krule *krule,
  1043. struct audit_parent *parent)
  1044. {
  1045. struct audit_watch *w, *watch = krule->watch;
  1046. int watch_found = 0;
  1047. list_for_each_entry(w, &parent->watches, wlist) {
  1048. if (strcmp(watch->path, w->path))
  1049. continue;
  1050. watch_found = 1;
  1051. /* put krule's and initial refs to temporary watch */
  1052. audit_put_watch(watch);
  1053. audit_put_watch(watch);
  1054. audit_get_watch(w);
  1055. krule->watch = watch = w;
  1056. break;
  1057. }
  1058. if (!watch_found) {
  1059. get_inotify_watch(&parent->wdata);
  1060. watch->parent = parent;
  1061. list_add(&watch->wlist, &parent->watches);
  1062. }
  1063. list_add(&krule->rlist, &watch->rules);
  1064. }
  1065. /* Find a matching watch entry, or add this one.
  1066. * Caller must hold audit_filter_mutex. */
  1067. static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
  1068. struct nameidata *ndw)
  1069. {
  1070. struct audit_watch *watch = krule->watch;
  1071. struct inotify_watch *i_watch;
  1072. struct audit_parent *parent;
  1073. int ret = 0;
  1074. /* update watch filter fields */
  1075. if (ndw) {
  1076. watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
  1077. watch->ino = ndw->path.dentry->d_inode->i_ino;
  1078. }
  1079. /* The audit_filter_mutex must not be held during inotify calls because
  1080. * we hold it during inotify event callback processing. If an existing
  1081. * inotify watch is found, inotify_find_watch() grabs a reference before
  1082. * returning.
  1083. */
  1084. mutex_unlock(&audit_filter_mutex);
  1085. if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
  1086. &i_watch) < 0) {
  1087. parent = audit_init_parent(ndp);
  1088. if (IS_ERR(parent)) {
  1089. /* caller expects mutex locked */
  1090. mutex_lock(&audit_filter_mutex);
  1091. return PTR_ERR(parent);
  1092. }
  1093. } else
  1094. parent = container_of(i_watch, struct audit_parent, wdata);
  1095. mutex_lock(&audit_filter_mutex);
  1096. /* parent was moved before we took audit_filter_mutex */
  1097. if (parent->flags & AUDIT_PARENT_INVALID)
  1098. ret = -ENOENT;
  1099. else
  1100. audit_add_to_parent(krule, parent);
  1101. /* match get in audit_init_parent or inotify_find_watch */
  1102. put_inotify_watch(&parent->wdata);
  1103. return ret;
  1104. }
  1105. /* Add rule to given filterlist if not a duplicate. */
  1106. static inline int audit_add_rule(struct audit_entry *entry,
  1107. struct list_head *list)
  1108. {
  1109. struct audit_entry *e;
  1110. struct audit_field *inode_f = entry->rule.inode_f;
  1111. struct audit_watch *watch = entry->rule.watch;
  1112. struct audit_tree *tree = entry->rule.tree;
  1113. struct nameidata *ndp = NULL, *ndw = NULL;
  1114. int h, err;
  1115. #ifdef CONFIG_AUDITSYSCALL
  1116. int dont_count = 0;
  1117. /* If either of these, don't count towards total */
  1118. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  1119. entry->rule.listnr == AUDIT_FILTER_TYPE)
  1120. dont_count = 1;
  1121. #endif
  1122. if (inode_f) {
  1123. h = audit_hash_ino(inode_f->val);
  1124. list = &audit_inode_hash[h];
  1125. }
  1126. mutex_lock(&audit_filter_mutex);
  1127. e = audit_find_rule(entry, list);
  1128. mutex_unlock(&audit_filter_mutex);
  1129. if (e) {
  1130. err = -EEXIST;
  1131. /* normally audit_add_tree_rule() will free it on failure */
  1132. if (tree)
  1133. audit_put_tree(tree);
  1134. goto error;
  1135. }
  1136. /* Avoid calling path_lookup under audit_filter_mutex. */
  1137. if (watch) {
  1138. err = audit_get_nd(watch->path, &ndp, &ndw);
  1139. if (err)
  1140. goto error;
  1141. }
  1142. mutex_lock(&audit_filter_mutex);
  1143. if (watch) {
  1144. /* audit_filter_mutex is dropped and re-taken during this call */
  1145. err = audit_add_watch(&entry->rule, ndp, ndw);
  1146. if (err) {
  1147. mutex_unlock(&audit_filter_mutex);
  1148. goto error;
  1149. }
  1150. h = audit_hash_ino((u32)watch->ino);
  1151. list = &audit_inode_hash[h];
  1152. }
  1153. if (tree) {
  1154. err = audit_add_tree_rule(&entry->rule);
  1155. if (err) {
  1156. mutex_unlock(&audit_filter_mutex);
  1157. goto error;
  1158. }
  1159. }
  1160. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  1161. list_add_rcu(&entry->list, list);
  1162. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  1163. } else {
  1164. list_add_tail_rcu(&entry->list, list);
  1165. }
  1166. #ifdef CONFIG_AUDITSYSCALL
  1167. if (!dont_count)
  1168. audit_n_rules++;
  1169. if (!audit_match_signal(entry))
  1170. audit_signals++;
  1171. #endif
  1172. mutex_unlock(&audit_filter_mutex);
  1173. audit_put_nd(ndp, ndw); /* NULL args OK */
  1174. return 0;
  1175. error:
  1176. audit_put_nd(ndp, ndw); /* NULL args OK */
  1177. if (watch)
  1178. audit_put_watch(watch); /* tmp watch, matches initial get */
  1179. return err;
  1180. }
  1181. /* Remove an existing rule from filterlist. */
  1182. static inline int audit_del_rule(struct audit_entry *entry,
  1183. struct list_head *list)
  1184. {
  1185. struct audit_entry *e;
  1186. struct audit_field *inode_f = entry->rule.inode_f;
  1187. struct audit_watch *watch, *tmp_watch = entry->rule.watch;
  1188. struct audit_tree *tree = entry->rule.tree;
  1189. LIST_HEAD(inotify_list);
  1190. int h, ret = 0;
  1191. #ifdef CONFIG_AUDITSYSCALL
  1192. int dont_count = 0;
  1193. /* If either of these, don't count towards total */
  1194. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  1195. entry->rule.listnr == AUDIT_FILTER_TYPE)
  1196. dont_count = 1;
  1197. #endif
  1198. if (inode_f) {
  1199. h = audit_hash_ino(inode_f->val);
  1200. list = &audit_inode_hash[h];
  1201. }
  1202. mutex_lock(&audit_filter_mutex);
  1203. e = audit_find_rule(entry, list);
  1204. if (!e) {
  1205. mutex_unlock(&audit_filter_mutex);
  1206. ret = -ENOENT;
  1207. goto out;
  1208. }
  1209. watch = e->rule.watch;
  1210. if (watch) {
  1211. struct audit_parent *parent = watch->parent;
  1212. list_del(&e->rule.rlist);
  1213. if (list_empty(&watch->rules)) {
  1214. audit_remove_watch(watch);
  1215. if (list_empty(&parent->watches)) {
  1216. /* Put parent on the inotify un-registration
  1217. * list. Grab a reference before releasing
  1218. * audit_filter_mutex, to be released in
  1219. * audit_inotify_unregister(). */
  1220. list_add(&parent->ilist, &inotify_list);
  1221. get_inotify_watch(&parent->wdata);
  1222. }
  1223. }
  1224. }
  1225. if (e->rule.tree)
  1226. audit_remove_tree_rule(&e->rule);
  1227. list_del_rcu(&e->list);
  1228. call_rcu(&e->rcu, audit_free_rule_rcu);
  1229. #ifdef CONFIG_AUDITSYSCALL
  1230. if (!dont_count)
  1231. audit_n_rules--;
  1232. if (!audit_match_signal(entry))
  1233. audit_signals--;
  1234. #endif
  1235. mutex_unlock(&audit_filter_mutex);
  1236. if (!list_empty(&inotify_list))
  1237. audit_inotify_unregister(&inotify_list);
  1238. out:
  1239. if (tmp_watch)
  1240. audit_put_watch(tmp_watch); /* match initial get */
  1241. if (tree)
  1242. audit_put_tree(tree); /* that's the temporary one */
  1243. return ret;
  1244. }
  1245. /* List rules using struct audit_rule. Exists for backward
  1246. * compatibility with userspace. */
  1247. static void audit_list(int pid, int seq, struct sk_buff_head *q)
  1248. {
  1249. struct sk_buff *skb;
  1250. struct audit_entry *entry;
  1251. int i;
  1252. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  1253. * iterator to sync with list writers. */
  1254. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  1255. list_for_each_entry(entry, &audit_filter_list[i], list) {
  1256. struct audit_rule *rule;
  1257. rule = audit_krule_to_rule(&entry->rule);
  1258. if (unlikely(!rule))
  1259. break;
  1260. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  1261. rule, sizeof(*rule));
  1262. if (skb)
  1263. skb_queue_tail(q, skb);
  1264. kfree(rule);
  1265. }
  1266. }
  1267. for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
  1268. list_for_each_entry(entry, &audit_inode_hash[i], list) {
  1269. struct audit_rule *rule;
  1270. rule = audit_krule_to_rule(&entry->rule);
  1271. if (unlikely(!rule))
  1272. break;
  1273. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  1274. rule, sizeof(*rule));
  1275. if (skb)
  1276. skb_queue_tail(q, skb);
  1277. kfree(rule);
  1278. }
  1279. }
  1280. skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  1281. if (skb)
  1282. skb_queue_tail(q, skb);
  1283. }
  1284. /* List rules using struct audit_rule_data. */
  1285. static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
  1286. {
  1287. struct sk_buff *skb;
  1288. struct audit_entry *e;
  1289. int i;
  1290. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  1291. * iterator to sync with list writers. */
  1292. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  1293. list_for_each_entry(e, &audit_filter_list[i], list) {
  1294. struct audit_rule_data *data;
  1295. data = audit_krule_to_data(&e->rule);
  1296. if (unlikely(!data))
  1297. break;
  1298. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  1299. data, sizeof(*data) + data->buflen);
  1300. if (skb)
  1301. skb_queue_tail(q, skb);
  1302. kfree(data);
  1303. }
  1304. }
  1305. for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
  1306. list_for_each_entry(e, &audit_inode_hash[i], list) {
  1307. struct audit_rule_data *data;
  1308. data = audit_krule_to_data(&e->rule);
  1309. if (unlikely(!data))
  1310. break;
  1311. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  1312. data, sizeof(*data) + data->buflen);
  1313. if (skb)
  1314. skb_queue_tail(q, skb);
  1315. kfree(data);
  1316. }
  1317. }
  1318. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  1319. if (skb)
  1320. skb_queue_tail(q, skb);
  1321. }
  1322. /* Log rule additions and removals */
  1323. static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
  1324. char *action, struct audit_krule *rule,
  1325. int res)
  1326. {
  1327. struct audit_buffer *ab;
  1328. if (!audit_enabled)
  1329. return;
  1330. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  1331. if (!ab)
  1332. return;
  1333. audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
  1334. if (sid) {
  1335. char *ctx = NULL;
  1336. u32 len;
  1337. if (security_secid_to_secctx(sid, &ctx, &len))
  1338. audit_log_format(ab, " ssid=%u", sid);
  1339. else {
  1340. audit_log_format(ab, " subj=%s", ctx);
  1341. security_release_secctx(ctx, len);
  1342. }
  1343. }
  1344. audit_log_format(ab, " op=%s rule key=", action);
  1345. if (rule->filterkey)
  1346. audit_log_untrustedstring(ab, rule->filterkey);
  1347. else
  1348. audit_log_format(ab, "(null)");
  1349. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  1350. audit_log_end(ab);
  1351. }
  1352. /**
  1353. * audit_receive_filter - apply all rules to the specified message type
  1354. * @type: audit message type
  1355. * @pid: target pid for netlink audit messages
  1356. * @uid: target uid for netlink audit messages
  1357. * @seq: netlink audit message sequence (serial) number
  1358. * @data: payload data
  1359. * @datasz: size of payload data
  1360. * @loginuid: loginuid of sender
  1361. * @sessionid: sessionid for netlink audit message
  1362. * @sid: SE Linux Security ID of sender
  1363. */
  1364. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  1365. size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
  1366. {
  1367. struct task_struct *tsk;
  1368. struct audit_netlink_list *dest;
  1369. int err = 0;
  1370. struct audit_entry *entry;
  1371. switch (type) {
  1372. case AUDIT_LIST:
  1373. case AUDIT_LIST_RULES:
  1374. /* We can't just spew out the rules here because we might fill
  1375. * the available socket buffer space and deadlock waiting for
  1376. * auditctl to read from it... which isn't ever going to
  1377. * happen if we're actually running in the context of auditctl
  1378. * trying to _send_ the stuff */
  1379. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1380. if (!dest)
  1381. return -ENOMEM;
  1382. dest->pid = pid;
  1383. skb_queue_head_init(&dest->q);
  1384. mutex_lock(&audit_filter_mutex);
  1385. if (type == AUDIT_LIST)
  1386. audit_list(pid, seq, &dest->q);
  1387. else
  1388. audit_list_rules(pid, seq, &dest->q);
  1389. mutex_unlock(&audit_filter_mutex);
  1390. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1391. if (IS_ERR(tsk)) {
  1392. skb_queue_purge(&dest->q);
  1393. kfree(dest);
  1394. err = PTR_ERR(tsk);
  1395. }
  1396. break;
  1397. case AUDIT_ADD:
  1398. case AUDIT_ADD_RULE:
  1399. if (type == AUDIT_ADD)
  1400. entry = audit_rule_to_entry(data);
  1401. else
  1402. entry = audit_data_to_entry(data, datasz);
  1403. if (IS_ERR(entry))
  1404. return PTR_ERR(entry);
  1405. err = audit_add_rule(entry,
  1406. &audit_filter_list[entry->rule.listnr]);
  1407. audit_log_rule_change(loginuid, sessionid, sid, "add",
  1408. &entry->rule, !err);
  1409. if (err)
  1410. audit_free_rule(entry);
  1411. break;
  1412. case AUDIT_DEL:
  1413. case AUDIT_DEL_RULE:
  1414. if (type == AUDIT_DEL)
  1415. entry = audit_rule_to_entry(data);
  1416. else
  1417. entry = audit_data_to_entry(data, datasz);
  1418. if (IS_ERR(entry))
  1419. return PTR_ERR(entry);
  1420. err = audit_del_rule(entry,
  1421. &audit_filter_list[entry->rule.listnr]);
  1422. audit_log_rule_change(loginuid, sessionid, sid, "remove",
  1423. &entry->rule, !err);
  1424. audit_free_rule(entry);
  1425. break;
  1426. default:
  1427. return -EINVAL;
  1428. }
  1429. return err;
  1430. }
  1431. int audit_comparator(const u32 left, const u32 op, const u32 right)
  1432. {
  1433. switch (op) {
  1434. case AUDIT_EQUAL:
  1435. return (left == right);
  1436. case AUDIT_NOT_EQUAL:
  1437. return (left != right);
  1438. case AUDIT_LESS_THAN:
  1439. return (left < right);
  1440. case AUDIT_LESS_THAN_OR_EQUAL:
  1441. return (left <= right);
  1442. case AUDIT_GREATER_THAN:
  1443. return (left > right);
  1444. case AUDIT_GREATER_THAN_OR_EQUAL:
  1445. return (left >= right);
  1446. case AUDIT_BIT_MASK:
  1447. return (left & right);
  1448. case AUDIT_BIT_TEST:
  1449. return ((left & right) == right);
  1450. }
  1451. BUG();
  1452. return 0;
  1453. }
  1454. /* Compare given dentry name with last component in given path,
  1455. * return of 0 indicates a match. */
  1456. int audit_compare_dname_path(const char *dname, const char *path,
  1457. int *dirlen)
  1458. {
  1459. int dlen, plen;
  1460. const char *p;
  1461. if (!dname || !path)
  1462. return 1;
  1463. dlen = strlen(dname);
  1464. plen = strlen(path);
  1465. if (plen < dlen)
  1466. return 1;
  1467. /* disregard trailing slashes */
  1468. p = path + plen - 1;
  1469. while ((*p == '/') && (p > path))
  1470. p--;
  1471. /* find last path component */
  1472. p = p - dlen + 1;
  1473. if (p < path)
  1474. return 1;
  1475. else if (p > path) {
  1476. if (*--p != '/')
  1477. return 1;
  1478. else
  1479. p++;
  1480. }
  1481. /* return length of path's directory component */
  1482. if (dirlen)
  1483. *dirlen = p - path;
  1484. return strncmp(p, dname, dlen);
  1485. }
  1486. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  1487. struct audit_krule *rule,
  1488. enum audit_state *state)
  1489. {
  1490. int i;
  1491. for (i = 0; i < rule->field_count; i++) {
  1492. struct audit_field *f = &rule->fields[i];
  1493. int result = 0;
  1494. switch (f->type) {
  1495. case AUDIT_PID:
  1496. result = audit_comparator(cb->creds.pid, f->op, f->val);
  1497. break;
  1498. case AUDIT_UID:
  1499. result = audit_comparator(cb->creds.uid, f->op, f->val);
  1500. break;
  1501. case AUDIT_GID:
  1502. result = audit_comparator(cb->creds.gid, f->op, f->val);
  1503. break;
  1504. case AUDIT_LOGINUID:
  1505. result = audit_comparator(cb->loginuid, f->op, f->val);
  1506. break;
  1507. }
  1508. if (!result)
  1509. return 0;
  1510. }
  1511. switch (rule->action) {
  1512. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  1513. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  1514. }
  1515. return 1;
  1516. }
  1517. int audit_filter_user(struct netlink_skb_parms *cb)
  1518. {
  1519. enum audit_state state = AUDIT_DISABLED;
  1520. struct audit_entry *e;
  1521. int ret = 1;
  1522. rcu_read_lock();
  1523. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  1524. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  1525. if (state == AUDIT_DISABLED)
  1526. ret = 0;
  1527. break;
  1528. }
  1529. }
  1530. rcu_read_unlock();
  1531. return ret; /* Audit by default */
  1532. }
  1533. int audit_filter_type(int type)
  1534. {
  1535. struct audit_entry *e;
  1536. int result = 0;
  1537. rcu_read_lock();
  1538. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  1539. goto unlock_and_return;
  1540. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  1541. list) {
  1542. int i;
  1543. for (i = 0; i < e->rule.field_count; i++) {
  1544. struct audit_field *f = &e->rule.fields[i];
  1545. if (f->type == AUDIT_MSGTYPE) {
  1546. result = audit_comparator(type, f->op, f->val);
  1547. if (!result)
  1548. break;
  1549. }
  1550. }
  1551. if (result)
  1552. goto unlock_and_return;
  1553. }
  1554. unlock_and_return:
  1555. rcu_read_unlock();
  1556. return result;
  1557. }
  1558. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1559. * It will traverse the filter lists serarching for rules that contain LSM
  1560. * specific filter fields. When such a rule is found, it is copied, the
  1561. * LSM field is re-initialized, and the old rule is replaced with the
  1562. * updated rule. */
  1563. int audit_update_lsm_rules(void)
  1564. {
  1565. struct audit_entry *entry, *n, *nentry;
  1566. struct audit_watch *watch;
  1567. struct audit_tree *tree;
  1568. int i, err = 0;
  1569. /* audit_filter_mutex synchronizes the writers */
  1570. mutex_lock(&audit_filter_mutex);
  1571. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1572. list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
  1573. if (!security_audit_rule_known(&entry->rule))
  1574. continue;
  1575. watch = entry->rule.watch;
  1576. tree = entry->rule.tree;
  1577. nentry = audit_dupe_rule(&entry->rule, watch);
  1578. if (IS_ERR(nentry)) {
  1579. /* save the first error encountered for the
  1580. * return value */
  1581. if (!err)
  1582. err = PTR_ERR(nentry);
  1583. audit_panic("error updating LSM filters");
  1584. if (watch)
  1585. list_del(&entry->rule.rlist);
  1586. list_del_rcu(&entry->list);
  1587. } else {
  1588. if (watch) {
  1589. list_add(&nentry->rule.rlist,
  1590. &watch->rules);
  1591. list_del(&entry->rule.rlist);
  1592. } else if (tree)
  1593. list_replace_init(&entry->rule.rlist,
  1594. &nentry->rule.rlist);
  1595. list_replace_rcu(&entry->list, &nentry->list);
  1596. }
  1597. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1598. }
  1599. }
  1600. mutex_unlock(&audit_filter_mutex);
  1601. return err;
  1602. }
  1603. /* Update watch data in audit rules based on inotify events. */
  1604. void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
  1605. u32 cookie, const char *dname, struct inode *inode)
  1606. {
  1607. struct audit_parent *parent;
  1608. parent = container_of(i_watch, struct audit_parent, wdata);
  1609. if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
  1610. audit_update_watch(parent, dname, inode->i_sb->s_dev,
  1611. inode->i_ino, 0);
  1612. else if (mask & (IN_DELETE|IN_MOVED_FROM))
  1613. audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
  1614. /* inotify automatically removes the watch and sends IN_IGNORED */
  1615. else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
  1616. audit_remove_parent_watches(parent);
  1617. /* inotify does not remove the watch, so remove it manually */
  1618. else if(mask & IN_MOVE_SELF) {
  1619. audit_remove_parent_watches(parent);
  1620. inotify_remove_watch_locked(audit_ih, i_watch);
  1621. } else if (mask & IN_IGNORED)
  1622. put_inotify_watch(i_watch);
  1623. }