auditfilter.c 45 KB

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