auditfilter.c 45 KB

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