auditfilter.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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/security.h>
  30. #include "audit.h"
  31. /*
  32. * Locking model:
  33. *
  34. * audit_filter_mutex:
  35. * Synchronizes writes and blocking reads of audit's filterlist
  36. * data. Rcu is used to traverse the filterlist and access
  37. * contents of structs audit_entry, audit_watch and opaque
  38. * LSM rules during filtering. If modified, these structures
  39. * must be copied and replace their counterparts in the filterlist.
  40. * An audit_parent struct is not accessed during filtering, so may
  41. * be written directly provided audit_filter_mutex is held.
  42. */
  43. /* Audit filter lists, defined in <linux/audit.h> */
  44. struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  45. LIST_HEAD_INIT(audit_filter_list[0]),
  46. LIST_HEAD_INIT(audit_filter_list[1]),
  47. LIST_HEAD_INIT(audit_filter_list[2]),
  48. LIST_HEAD_INIT(audit_filter_list[3]),
  49. LIST_HEAD_INIT(audit_filter_list[4]),
  50. LIST_HEAD_INIT(audit_filter_list[5]),
  51. #if AUDIT_NR_FILTERS != 6
  52. #error Fix audit_filter_list initialiser
  53. #endif
  54. };
  55. static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
  56. LIST_HEAD_INIT(audit_rules_list[0]),
  57. LIST_HEAD_INIT(audit_rules_list[1]),
  58. LIST_HEAD_INIT(audit_rules_list[2]),
  59. LIST_HEAD_INIT(audit_rules_list[3]),
  60. LIST_HEAD_INIT(audit_rules_list[4]),
  61. LIST_HEAD_INIT(audit_rules_list[5]),
  62. };
  63. DEFINE_MUTEX(audit_filter_mutex);
  64. static inline void audit_free_rule(struct audit_entry *e)
  65. {
  66. int i;
  67. struct audit_krule *erule = &e->rule;
  68. /* some rules don't have associated watches */
  69. if (erule->watch)
  70. audit_put_watch(erule->watch);
  71. if (erule->fields)
  72. for (i = 0; i < erule->field_count; i++) {
  73. struct audit_field *f = &erule->fields[i];
  74. kfree(f->lsm_str);
  75. security_audit_rule_free(f->lsm_rule);
  76. }
  77. kfree(erule->fields);
  78. kfree(erule->filterkey);
  79. kfree(e);
  80. }
  81. void audit_free_rule_rcu(struct rcu_head *head)
  82. {
  83. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  84. audit_free_rule(e);
  85. }
  86. /* Initialize an audit filterlist entry. */
  87. static inline struct audit_entry *audit_init_entry(u32 field_count)
  88. {
  89. struct audit_entry *entry;
  90. struct audit_field *fields;
  91. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  92. if (unlikely(!entry))
  93. return NULL;
  94. fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
  95. if (unlikely(!fields)) {
  96. kfree(entry);
  97. return NULL;
  98. }
  99. entry->rule.fields = fields;
  100. return entry;
  101. }
  102. /* Unpack a filter field's string representation from user-space
  103. * buffer. */
  104. char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
  105. {
  106. char *str;
  107. if (!*bufp || (len == 0) || (len > *remain))
  108. return ERR_PTR(-EINVAL);
  109. /* Of the currently implemented string fields, PATH_MAX
  110. * defines the longest valid length.
  111. */
  112. if (len > PATH_MAX)
  113. return ERR_PTR(-ENAMETOOLONG);
  114. str = kmalloc(len + 1, GFP_KERNEL);
  115. if (unlikely(!str))
  116. return ERR_PTR(-ENOMEM);
  117. memcpy(str, *bufp, len);
  118. str[len] = 0;
  119. *bufp += len;
  120. *remain -= len;
  121. return str;
  122. }
  123. /* Translate an inode field to kernel respresentation. */
  124. static inline int audit_to_inode(struct audit_krule *krule,
  125. struct audit_field *f)
  126. {
  127. if (krule->listnr != AUDIT_FILTER_EXIT ||
  128. krule->watch || krule->inode_f || krule->tree ||
  129. (f->op != Audit_equal && f->op != Audit_not_equal))
  130. return -EINVAL;
  131. krule->inode_f = f;
  132. return 0;
  133. }
  134. static __u32 *classes[AUDIT_SYSCALL_CLASSES];
  135. int __init audit_register_class(int class, unsigned *list)
  136. {
  137. __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
  138. if (!p)
  139. return -ENOMEM;
  140. while (*list != ~0U) {
  141. unsigned n = *list++;
  142. if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
  143. kfree(p);
  144. return -EINVAL;
  145. }
  146. p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
  147. }
  148. if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
  149. kfree(p);
  150. return -EINVAL;
  151. }
  152. classes[class] = p;
  153. return 0;
  154. }
  155. int audit_match_class(int class, unsigned syscall)
  156. {
  157. if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
  158. return 0;
  159. if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
  160. return 0;
  161. return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
  162. }
  163. #ifdef CONFIG_AUDITSYSCALL
  164. static inline int audit_match_class_bits(int class, u32 *mask)
  165. {
  166. int i;
  167. if (classes[class]) {
  168. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  169. if (mask[i] & classes[class][i])
  170. return 0;
  171. }
  172. return 1;
  173. }
  174. static int audit_match_signal(struct audit_entry *entry)
  175. {
  176. struct audit_field *arch = entry->rule.arch_f;
  177. if (!arch) {
  178. /* When arch is unspecified, we must check both masks on biarch
  179. * as syscall number alone is ambiguous. */
  180. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  181. entry->rule.mask) &&
  182. audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  183. entry->rule.mask));
  184. }
  185. switch(audit_classify_arch(arch->val)) {
  186. case 0: /* native */
  187. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
  188. entry->rule.mask));
  189. case 1: /* 32bit on biarch */
  190. return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
  191. entry->rule.mask));
  192. default:
  193. return 1;
  194. }
  195. }
  196. #endif
  197. /* Common user-space to kernel rule translation. */
  198. static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
  199. {
  200. unsigned listnr;
  201. struct audit_entry *entry;
  202. int i, err;
  203. err = -EINVAL;
  204. listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
  205. switch(listnr) {
  206. default:
  207. goto exit_err;
  208. case AUDIT_FILTER_USER:
  209. case AUDIT_FILTER_TYPE:
  210. #ifdef CONFIG_AUDITSYSCALL
  211. case AUDIT_FILTER_ENTRY:
  212. case AUDIT_FILTER_EXIT:
  213. case AUDIT_FILTER_TASK:
  214. #endif
  215. ;
  216. }
  217. if (unlikely(rule->action == AUDIT_POSSIBLE)) {
  218. printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
  219. goto exit_err;
  220. }
  221. if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
  222. goto exit_err;
  223. if (rule->field_count > AUDIT_MAX_FIELDS)
  224. goto exit_err;
  225. err = -ENOMEM;
  226. entry = audit_init_entry(rule->field_count);
  227. if (!entry)
  228. goto exit_err;
  229. entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
  230. entry->rule.listnr = listnr;
  231. entry->rule.action = rule->action;
  232. entry->rule.field_count = rule->field_count;
  233. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  234. entry->rule.mask[i] = rule->mask[i];
  235. for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
  236. int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
  237. __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
  238. __u32 *class;
  239. if (!(*p & AUDIT_BIT(bit)))
  240. continue;
  241. *p &= ~AUDIT_BIT(bit);
  242. class = classes[i];
  243. if (class) {
  244. int j;
  245. for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
  246. entry->rule.mask[j] |= class[j];
  247. }
  248. }
  249. return entry;
  250. exit_err:
  251. return ERR_PTR(err);
  252. }
  253. static u32 audit_ops[] =
  254. {
  255. [Audit_equal] = AUDIT_EQUAL,
  256. [Audit_not_equal] = AUDIT_NOT_EQUAL,
  257. [Audit_bitmask] = AUDIT_BIT_MASK,
  258. [Audit_bittest] = AUDIT_BIT_TEST,
  259. [Audit_lt] = AUDIT_LESS_THAN,
  260. [Audit_gt] = AUDIT_GREATER_THAN,
  261. [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL,
  262. [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL,
  263. };
  264. static u32 audit_to_op(u32 op)
  265. {
  266. u32 n;
  267. for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
  268. ;
  269. return n;
  270. }
  271. /* Translate struct audit_rule to kernel's rule respresentation.
  272. * Exists for backward compatibility with userspace. */
  273. static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
  274. {
  275. struct audit_entry *entry;
  276. int err = 0;
  277. int i;
  278. entry = audit_to_entry_common(rule);
  279. if (IS_ERR(entry))
  280. goto exit_nofree;
  281. for (i = 0; i < rule->field_count; i++) {
  282. struct audit_field *f = &entry->rule.fields[i];
  283. u32 n;
  284. n = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
  285. /* Support for legacy operators where
  286. * AUDIT_NEGATE bit signifies != and otherwise assumes == */
  287. if (n & AUDIT_NEGATE)
  288. f->op = Audit_not_equal;
  289. else if (!n)
  290. f->op = Audit_equal;
  291. else
  292. f->op = audit_to_op(n);
  293. entry->rule.vers_ops = (n & AUDIT_OPERATORS) ? 2 : 1;
  294. f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
  295. f->val = rule->values[i];
  296. err = -EINVAL;
  297. if (f->op == Audit_bad)
  298. goto exit_free;
  299. switch(f->type) {
  300. default:
  301. goto exit_free;
  302. case AUDIT_PID:
  303. case AUDIT_UID:
  304. case AUDIT_EUID:
  305. case AUDIT_SUID:
  306. case AUDIT_FSUID:
  307. case AUDIT_GID:
  308. case AUDIT_EGID:
  309. case AUDIT_SGID:
  310. case AUDIT_FSGID:
  311. case AUDIT_LOGINUID:
  312. case AUDIT_PERS:
  313. case AUDIT_MSGTYPE:
  314. case AUDIT_PPID:
  315. case AUDIT_DEVMAJOR:
  316. case AUDIT_DEVMINOR:
  317. case AUDIT_EXIT:
  318. case AUDIT_SUCCESS:
  319. /* bit ops are only useful on syscall args */
  320. if (f->op == Audit_bitmask || f->op == Audit_bittest)
  321. goto exit_free;
  322. break;
  323. case AUDIT_ARG0:
  324. case AUDIT_ARG1:
  325. case AUDIT_ARG2:
  326. case AUDIT_ARG3:
  327. break;
  328. /* arch is only allowed to be = or != */
  329. case AUDIT_ARCH:
  330. if (f->op != Audit_not_equal && f->op != Audit_equal)
  331. goto exit_free;
  332. entry->rule.arch_f = f;
  333. break;
  334. case AUDIT_PERM:
  335. if (f->val & ~15)
  336. goto exit_free;
  337. break;
  338. case AUDIT_FILETYPE:
  339. if ((f->val & ~S_IFMT) > S_IFMT)
  340. goto exit_free;
  341. break;
  342. case AUDIT_INODE:
  343. err = audit_to_inode(&entry->rule, f);
  344. if (err)
  345. goto exit_free;
  346. break;
  347. }
  348. }
  349. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  350. entry->rule.inode_f = NULL;
  351. exit_nofree:
  352. return entry;
  353. exit_free:
  354. audit_free_rule(entry);
  355. return ERR_PTR(err);
  356. }
  357. /* Translate struct audit_rule_data to kernel's rule respresentation. */
  358. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  359. size_t datasz)
  360. {
  361. int err = 0;
  362. struct audit_entry *entry;
  363. void *bufp;
  364. size_t remain = datasz - sizeof(struct audit_rule_data);
  365. int i;
  366. char *str;
  367. entry = audit_to_entry_common((struct audit_rule *)data);
  368. if (IS_ERR(entry))
  369. goto exit_nofree;
  370. bufp = data->buf;
  371. entry->rule.vers_ops = 2;
  372. for (i = 0; i < data->field_count; i++) {
  373. struct audit_field *f = &entry->rule.fields[i];
  374. err = -EINVAL;
  375. f->op = audit_to_op(data->fieldflags[i]);
  376. if (f->op == Audit_bad)
  377. goto exit_free;
  378. f->type = data->fields[i];
  379. f->val = data->values[i];
  380. f->lsm_str = NULL;
  381. f->lsm_rule = NULL;
  382. switch(f->type) {
  383. case AUDIT_PID:
  384. case AUDIT_UID:
  385. case AUDIT_EUID:
  386. case AUDIT_SUID:
  387. case AUDIT_FSUID:
  388. case AUDIT_GID:
  389. case AUDIT_EGID:
  390. case AUDIT_SGID:
  391. case AUDIT_FSGID:
  392. case AUDIT_LOGINUID:
  393. case AUDIT_PERS:
  394. case AUDIT_MSGTYPE:
  395. case AUDIT_PPID:
  396. case AUDIT_DEVMAJOR:
  397. case AUDIT_DEVMINOR:
  398. case AUDIT_EXIT:
  399. case AUDIT_SUCCESS:
  400. case AUDIT_ARG0:
  401. case AUDIT_ARG1:
  402. case AUDIT_ARG2:
  403. case AUDIT_ARG3:
  404. break;
  405. case AUDIT_ARCH:
  406. entry->rule.arch_f = f;
  407. break;
  408. case AUDIT_SUBJ_USER:
  409. case AUDIT_SUBJ_ROLE:
  410. case AUDIT_SUBJ_TYPE:
  411. case AUDIT_SUBJ_SEN:
  412. case AUDIT_SUBJ_CLR:
  413. case AUDIT_OBJ_USER:
  414. case AUDIT_OBJ_ROLE:
  415. case AUDIT_OBJ_TYPE:
  416. case AUDIT_OBJ_LEV_LOW:
  417. case AUDIT_OBJ_LEV_HIGH:
  418. str = audit_unpack_string(&bufp, &remain, f->val);
  419. if (IS_ERR(str))
  420. goto exit_free;
  421. entry->rule.buflen += f->val;
  422. err = security_audit_rule_init(f->type, f->op, str,
  423. (void **)&f->lsm_rule);
  424. /* Keep currently invalid fields around in case they
  425. * become valid after a policy reload. */
  426. if (err == -EINVAL) {
  427. printk(KERN_WARNING "audit rule for LSM "
  428. "\'%s\' is invalid\n", str);
  429. err = 0;
  430. }
  431. if (err) {
  432. kfree(str);
  433. goto exit_free;
  434. } else
  435. f->lsm_str = str;
  436. break;
  437. case AUDIT_WATCH:
  438. str = audit_unpack_string(&bufp, &remain, f->val);
  439. if (IS_ERR(str))
  440. goto exit_free;
  441. entry->rule.buflen += f->val;
  442. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  443. if (err) {
  444. kfree(str);
  445. goto exit_free;
  446. }
  447. break;
  448. case AUDIT_DIR:
  449. str = audit_unpack_string(&bufp, &remain, f->val);
  450. if (IS_ERR(str))
  451. goto exit_free;
  452. entry->rule.buflen += f->val;
  453. err = audit_make_tree(&entry->rule, str, f->op);
  454. kfree(str);
  455. if (err)
  456. goto exit_free;
  457. break;
  458. case AUDIT_INODE:
  459. err = audit_to_inode(&entry->rule, f);
  460. if (err)
  461. goto exit_free;
  462. break;
  463. case AUDIT_FILTERKEY:
  464. err = -EINVAL;
  465. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  466. goto exit_free;
  467. str = audit_unpack_string(&bufp, &remain, f->val);
  468. if (IS_ERR(str))
  469. goto exit_free;
  470. entry->rule.buflen += f->val;
  471. entry->rule.filterkey = str;
  472. break;
  473. case AUDIT_PERM:
  474. if (f->val & ~15)
  475. goto exit_free;
  476. break;
  477. case AUDIT_FILETYPE:
  478. if ((f->val & ~S_IFMT) > S_IFMT)
  479. goto exit_free;
  480. break;
  481. default:
  482. goto exit_free;
  483. }
  484. }
  485. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  486. entry->rule.inode_f = NULL;
  487. exit_nofree:
  488. return entry;
  489. exit_free:
  490. audit_free_rule(entry);
  491. return ERR_PTR(err);
  492. }
  493. /* Pack a filter field's string representation into data block. */
  494. static inline size_t audit_pack_string(void **bufp, const char *str)
  495. {
  496. size_t len = strlen(str);
  497. memcpy(*bufp, str, len);
  498. *bufp += len;
  499. return len;
  500. }
  501. /* Translate kernel rule respresentation to struct audit_rule.
  502. * Exists for backward compatibility with userspace. */
  503. static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
  504. {
  505. struct audit_rule *rule;
  506. int i;
  507. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  508. if (unlikely(!rule))
  509. return NULL;
  510. rule->flags = krule->flags | krule->listnr;
  511. rule->action = krule->action;
  512. rule->field_count = krule->field_count;
  513. for (i = 0; i < rule->field_count; i++) {
  514. rule->values[i] = krule->fields[i].val;
  515. rule->fields[i] = krule->fields[i].type;
  516. if (krule->vers_ops == 1) {
  517. if (krule->fields[i].op == Audit_not_equal)
  518. rule->fields[i] |= AUDIT_NEGATE;
  519. } else {
  520. rule->fields[i] |= audit_ops[krule->fields[i].op];
  521. }
  522. }
  523. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
  524. return rule;
  525. }
  526. /* Translate kernel rule respresentation to struct audit_rule_data. */
  527. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  528. {
  529. struct audit_rule_data *data;
  530. void *bufp;
  531. int i;
  532. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  533. if (unlikely(!data))
  534. return NULL;
  535. memset(data, 0, sizeof(*data));
  536. data->flags = krule->flags | krule->listnr;
  537. data->action = krule->action;
  538. data->field_count = krule->field_count;
  539. bufp = data->buf;
  540. for (i = 0; i < data->field_count; i++) {
  541. struct audit_field *f = &krule->fields[i];
  542. data->fields[i] = f->type;
  543. data->fieldflags[i] = audit_ops[f->op];
  544. switch(f->type) {
  545. case AUDIT_SUBJ_USER:
  546. case AUDIT_SUBJ_ROLE:
  547. case AUDIT_SUBJ_TYPE:
  548. case AUDIT_SUBJ_SEN:
  549. case AUDIT_SUBJ_CLR:
  550. case AUDIT_OBJ_USER:
  551. case AUDIT_OBJ_ROLE:
  552. case AUDIT_OBJ_TYPE:
  553. case AUDIT_OBJ_LEV_LOW:
  554. case AUDIT_OBJ_LEV_HIGH:
  555. data->buflen += data->values[i] =
  556. audit_pack_string(&bufp, f->lsm_str);
  557. break;
  558. case AUDIT_WATCH:
  559. data->buflen += data->values[i] =
  560. audit_pack_string(&bufp,
  561. audit_watch_path(krule->watch));
  562. break;
  563. case AUDIT_DIR:
  564. data->buflen += data->values[i] =
  565. audit_pack_string(&bufp,
  566. audit_tree_path(krule->tree));
  567. break;
  568. case AUDIT_FILTERKEY:
  569. data->buflen += data->values[i] =
  570. audit_pack_string(&bufp, krule->filterkey);
  571. break;
  572. default:
  573. data->values[i] = f->val;
  574. }
  575. }
  576. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  577. return data;
  578. }
  579. /* Compare two rules in kernel format. Considered success if rules
  580. * don't match. */
  581. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  582. {
  583. int i;
  584. if (a->flags != b->flags ||
  585. a->listnr != b->listnr ||
  586. a->action != b->action ||
  587. a->field_count != b->field_count)
  588. return 1;
  589. for (i = 0; i < a->field_count; i++) {
  590. if (a->fields[i].type != b->fields[i].type ||
  591. a->fields[i].op != b->fields[i].op)
  592. return 1;
  593. switch(a->fields[i].type) {
  594. case AUDIT_SUBJ_USER:
  595. case AUDIT_SUBJ_ROLE:
  596. case AUDIT_SUBJ_TYPE:
  597. case AUDIT_SUBJ_SEN:
  598. case AUDIT_SUBJ_CLR:
  599. case AUDIT_OBJ_USER:
  600. case AUDIT_OBJ_ROLE:
  601. case AUDIT_OBJ_TYPE:
  602. case AUDIT_OBJ_LEV_LOW:
  603. case AUDIT_OBJ_LEV_HIGH:
  604. if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
  605. return 1;
  606. break;
  607. case AUDIT_WATCH:
  608. if (strcmp(audit_watch_path(a->watch),
  609. audit_watch_path(b->watch)))
  610. return 1;
  611. break;
  612. case AUDIT_DIR:
  613. if (strcmp(audit_tree_path(a->tree),
  614. audit_tree_path(b->tree)))
  615. return 1;
  616. break;
  617. case AUDIT_FILTERKEY:
  618. /* both filterkeys exist based on above type compare */
  619. if (strcmp(a->filterkey, b->filterkey))
  620. return 1;
  621. break;
  622. default:
  623. if (a->fields[i].val != b->fields[i].val)
  624. return 1;
  625. }
  626. }
  627. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  628. if (a->mask[i] != b->mask[i])
  629. return 1;
  630. return 0;
  631. }
  632. /* Duplicate LSM field information. The lsm_rule is opaque, so must be
  633. * re-initialized. */
  634. static inline int audit_dupe_lsm_field(struct audit_field *df,
  635. struct audit_field *sf)
  636. {
  637. int ret = 0;
  638. char *lsm_str;
  639. /* our own copy of lsm_str */
  640. lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
  641. if (unlikely(!lsm_str))
  642. return -ENOMEM;
  643. df->lsm_str = lsm_str;
  644. /* our own (refreshed) copy of lsm_rule */
  645. ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
  646. (void **)&df->lsm_rule);
  647. /* Keep currently invalid fields around in case they
  648. * become valid after a policy reload. */
  649. if (ret == -EINVAL) {
  650. printk(KERN_WARNING "audit rule for LSM \'%s\' is "
  651. "invalid\n", df->lsm_str);
  652. ret = 0;
  653. }
  654. return ret;
  655. }
  656. /* Duplicate an audit rule. This will be a deep copy with the exception
  657. * of the watch - that pointer is carried over. The LSM specific fields
  658. * will be updated in the copy. The point is to be able to replace the old
  659. * rule with the new rule in the filterlist, then free the old rule.
  660. * The rlist element is undefined; list manipulations are handled apart from
  661. * the initial copy. */
  662. struct audit_entry *audit_dupe_rule(struct audit_krule *old,
  663. struct audit_watch *watch)
  664. {
  665. u32 fcount = old->field_count;
  666. struct audit_entry *entry;
  667. struct audit_krule *new;
  668. char *fk;
  669. int i, err = 0;
  670. entry = audit_init_entry(fcount);
  671. if (unlikely(!entry))
  672. return ERR_PTR(-ENOMEM);
  673. new = &entry->rule;
  674. new->vers_ops = old->vers_ops;
  675. new->flags = old->flags;
  676. new->listnr = old->listnr;
  677. new->action = old->action;
  678. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  679. new->mask[i] = old->mask[i];
  680. new->prio = old->prio;
  681. new->buflen = old->buflen;
  682. new->inode_f = old->inode_f;
  683. new->watch = NULL;
  684. new->field_count = old->field_count;
  685. /*
  686. * note that we are OK with not refcounting here; audit_match_tree()
  687. * never dereferences tree and we can't get false positives there
  688. * since we'd have to have rule gone from the list *and* removed
  689. * before the chunks found by lookup had been allocated, i.e. before
  690. * the beginning of list scan.
  691. */
  692. new->tree = old->tree;
  693. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  694. /* deep copy this information, updating the lsm_rule fields, because
  695. * the originals will all be freed when the old rule is freed. */
  696. for (i = 0; i < fcount; i++) {
  697. switch (new->fields[i].type) {
  698. case AUDIT_SUBJ_USER:
  699. case AUDIT_SUBJ_ROLE:
  700. case AUDIT_SUBJ_TYPE:
  701. case AUDIT_SUBJ_SEN:
  702. case AUDIT_SUBJ_CLR:
  703. case AUDIT_OBJ_USER:
  704. case AUDIT_OBJ_ROLE:
  705. case AUDIT_OBJ_TYPE:
  706. case AUDIT_OBJ_LEV_LOW:
  707. case AUDIT_OBJ_LEV_HIGH:
  708. err = audit_dupe_lsm_field(&new->fields[i],
  709. &old->fields[i]);
  710. break;
  711. case AUDIT_FILTERKEY:
  712. fk = kstrdup(old->filterkey, GFP_KERNEL);
  713. if (unlikely(!fk))
  714. err = -ENOMEM;
  715. else
  716. new->filterkey = fk;
  717. }
  718. if (err) {
  719. audit_free_rule(entry);
  720. return ERR_PTR(err);
  721. }
  722. }
  723. if (watch) {
  724. audit_get_watch(watch);
  725. new->watch = watch;
  726. }
  727. return entry;
  728. }
  729. /* Find an existing audit rule.
  730. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  731. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  732. struct list_head **p)
  733. {
  734. struct audit_entry *e, *found = NULL;
  735. struct list_head *list;
  736. int h;
  737. if (entry->rule.inode_f) {
  738. h = audit_hash_ino(entry->rule.inode_f->val);
  739. *p = list = &audit_inode_hash[h];
  740. } else if (entry->rule.watch) {
  741. /* we don't know the inode number, so must walk entire hash */
  742. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  743. list = &audit_inode_hash[h];
  744. list_for_each_entry(e, list, list)
  745. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  746. found = e;
  747. goto out;
  748. }
  749. }
  750. goto out;
  751. } else {
  752. *p = list = &audit_filter_list[entry->rule.listnr];
  753. }
  754. list_for_each_entry(e, list, list)
  755. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  756. found = e;
  757. goto out;
  758. }
  759. out:
  760. return found;
  761. }
  762. static u64 prio_low = ~0ULL/2;
  763. static u64 prio_high = ~0ULL/2 - 1;
  764. /* Add rule to given filterlist if not a duplicate. */
  765. static inline int audit_add_rule(struct audit_entry *entry)
  766. {
  767. struct audit_entry *e;
  768. struct audit_watch *watch = entry->rule.watch;
  769. struct audit_tree *tree = entry->rule.tree;
  770. struct nameidata *ndp = NULL, *ndw = NULL;
  771. struct list_head *list;
  772. int h, err;
  773. #ifdef CONFIG_AUDITSYSCALL
  774. int dont_count = 0;
  775. /* If either of these, don't count towards total */
  776. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  777. entry->rule.listnr == AUDIT_FILTER_TYPE)
  778. dont_count = 1;
  779. #endif
  780. mutex_lock(&audit_filter_mutex);
  781. e = audit_find_rule(entry, &list);
  782. mutex_unlock(&audit_filter_mutex);
  783. if (e) {
  784. err = -EEXIST;
  785. /* normally audit_add_tree_rule() will free it on failure */
  786. if (tree)
  787. audit_put_tree(tree);
  788. goto error;
  789. }
  790. /* Avoid calling path_lookup under audit_filter_mutex. */
  791. if (watch) {
  792. err = audit_get_nd(audit_watch_path(watch), &ndp, &ndw);
  793. if (err)
  794. goto error;
  795. }
  796. mutex_lock(&audit_filter_mutex);
  797. if (watch) {
  798. /* audit_filter_mutex is dropped and re-taken during this call */
  799. err = audit_add_watch(&entry->rule, ndp, ndw);
  800. if (err) {
  801. mutex_unlock(&audit_filter_mutex);
  802. goto error;
  803. }
  804. /* entry->rule.watch may have changed during audit_add_watch() */
  805. watch = entry->rule.watch;
  806. h = audit_hash_ino((u32)audit_watch_inode(watch));
  807. list = &audit_inode_hash[h];
  808. }
  809. if (tree) {
  810. err = audit_add_tree_rule(&entry->rule);
  811. if (err) {
  812. mutex_unlock(&audit_filter_mutex);
  813. goto error;
  814. }
  815. }
  816. entry->rule.prio = ~0ULL;
  817. if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
  818. if (entry->rule.flags & AUDIT_FILTER_PREPEND)
  819. entry->rule.prio = ++prio_high;
  820. else
  821. entry->rule.prio = --prio_low;
  822. }
  823. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  824. list_add(&entry->rule.list,
  825. &audit_rules_list[entry->rule.listnr]);
  826. list_add_rcu(&entry->list, list);
  827. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  828. } else {
  829. list_add_tail(&entry->rule.list,
  830. &audit_rules_list[entry->rule.listnr]);
  831. list_add_tail_rcu(&entry->list, list);
  832. }
  833. #ifdef CONFIG_AUDITSYSCALL
  834. if (!dont_count)
  835. audit_n_rules++;
  836. if (!audit_match_signal(entry))
  837. audit_signals++;
  838. #endif
  839. mutex_unlock(&audit_filter_mutex);
  840. audit_put_nd(ndp, ndw); /* NULL args OK */
  841. return 0;
  842. error:
  843. audit_put_nd(ndp, ndw); /* NULL args OK */
  844. if (watch)
  845. audit_put_watch(watch); /* tmp watch, matches initial get */
  846. return err;
  847. }
  848. /* Remove an existing rule from filterlist. */
  849. static inline int audit_del_rule(struct audit_entry *entry)
  850. {
  851. struct audit_entry *e;
  852. struct audit_watch *watch = entry->rule.watch;
  853. struct audit_tree *tree = entry->rule.tree;
  854. struct list_head *list;
  855. LIST_HEAD(inotify_list);
  856. int ret = 0;
  857. #ifdef CONFIG_AUDITSYSCALL
  858. int dont_count = 0;
  859. /* If either of these, don't count towards total */
  860. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  861. entry->rule.listnr == AUDIT_FILTER_TYPE)
  862. dont_count = 1;
  863. #endif
  864. mutex_lock(&audit_filter_mutex);
  865. e = audit_find_rule(entry, &list);
  866. if (!e) {
  867. mutex_unlock(&audit_filter_mutex);
  868. ret = -ENOENT;
  869. goto out;
  870. }
  871. if (e->rule.watch)
  872. audit_remove_watch_rule(&e->rule, &inotify_list);
  873. if (e->rule.tree)
  874. audit_remove_tree_rule(&e->rule);
  875. list_del_rcu(&e->list);
  876. list_del(&e->rule.list);
  877. call_rcu(&e->rcu, audit_free_rule_rcu);
  878. #ifdef CONFIG_AUDITSYSCALL
  879. if (!dont_count)
  880. audit_n_rules--;
  881. if (!audit_match_signal(entry))
  882. audit_signals--;
  883. #endif
  884. mutex_unlock(&audit_filter_mutex);
  885. if (!list_empty(&inotify_list))
  886. audit_inotify_unregister(&inotify_list);
  887. out:
  888. if (watch)
  889. audit_put_watch(watch); /* match initial get */
  890. if (tree)
  891. audit_put_tree(tree); /* that's the temporary one */
  892. return ret;
  893. }
  894. /* List rules using struct audit_rule. Exists for backward
  895. * compatibility with userspace. */
  896. static void audit_list(int pid, int seq, struct sk_buff_head *q)
  897. {
  898. struct sk_buff *skb;
  899. struct audit_krule *r;
  900. int i;
  901. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  902. * iterator to sync with list writers. */
  903. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  904. list_for_each_entry(r, &audit_rules_list[i], list) {
  905. struct audit_rule *rule;
  906. rule = audit_krule_to_rule(r);
  907. if (unlikely(!rule))
  908. break;
  909. skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
  910. rule, sizeof(*rule));
  911. if (skb)
  912. skb_queue_tail(q, skb);
  913. kfree(rule);
  914. }
  915. }
  916. skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  917. if (skb)
  918. skb_queue_tail(q, skb);
  919. }
  920. /* List rules using struct audit_rule_data. */
  921. static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
  922. {
  923. struct sk_buff *skb;
  924. struct audit_krule *r;
  925. int i;
  926. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  927. * iterator to sync with list writers. */
  928. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  929. list_for_each_entry(r, &audit_rules_list[i], list) {
  930. struct audit_rule_data *data;
  931. data = audit_krule_to_data(r);
  932. if (unlikely(!data))
  933. break;
  934. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
  935. data, sizeof(*data) + data->buflen);
  936. if (skb)
  937. skb_queue_tail(q, skb);
  938. kfree(data);
  939. }
  940. }
  941. skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  942. if (skb)
  943. skb_queue_tail(q, skb);
  944. }
  945. /* Log rule additions and removals */
  946. static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
  947. char *action, struct audit_krule *rule,
  948. int res)
  949. {
  950. struct audit_buffer *ab;
  951. if (!audit_enabled)
  952. return;
  953. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  954. if (!ab)
  955. return;
  956. audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
  957. if (sid) {
  958. char *ctx = NULL;
  959. u32 len;
  960. if (security_secid_to_secctx(sid, &ctx, &len))
  961. audit_log_format(ab, " ssid=%u", sid);
  962. else {
  963. audit_log_format(ab, " subj=%s", ctx);
  964. security_release_secctx(ctx, len);
  965. }
  966. }
  967. audit_log_format(ab, " op=%s rule key=", action);
  968. if (rule->filterkey)
  969. audit_log_untrustedstring(ab, rule->filterkey);
  970. else
  971. audit_log_format(ab, "(null)");
  972. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  973. audit_log_end(ab);
  974. }
  975. /**
  976. * audit_receive_filter - apply all rules to the specified message type
  977. * @type: audit message type
  978. * @pid: target pid for netlink audit messages
  979. * @uid: target uid for netlink audit messages
  980. * @seq: netlink audit message sequence (serial) number
  981. * @data: payload data
  982. * @datasz: size of payload data
  983. * @loginuid: loginuid of sender
  984. * @sessionid: sessionid for netlink audit message
  985. * @sid: SE Linux Security ID of sender
  986. */
  987. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  988. size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
  989. {
  990. struct task_struct *tsk;
  991. struct audit_netlink_list *dest;
  992. int err = 0;
  993. struct audit_entry *entry;
  994. switch (type) {
  995. case AUDIT_LIST:
  996. case AUDIT_LIST_RULES:
  997. /* We can't just spew out the rules here because we might fill
  998. * the available socket buffer space and deadlock waiting for
  999. * auditctl to read from it... which isn't ever going to
  1000. * happen if we're actually running in the context of auditctl
  1001. * trying to _send_ the stuff */
  1002. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1003. if (!dest)
  1004. return -ENOMEM;
  1005. dest->pid = pid;
  1006. skb_queue_head_init(&dest->q);
  1007. mutex_lock(&audit_filter_mutex);
  1008. if (type == AUDIT_LIST)
  1009. audit_list(pid, seq, &dest->q);
  1010. else
  1011. audit_list_rules(pid, seq, &dest->q);
  1012. mutex_unlock(&audit_filter_mutex);
  1013. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1014. if (IS_ERR(tsk)) {
  1015. skb_queue_purge(&dest->q);
  1016. kfree(dest);
  1017. err = PTR_ERR(tsk);
  1018. }
  1019. break;
  1020. case AUDIT_ADD:
  1021. case AUDIT_ADD_RULE:
  1022. if (type == AUDIT_ADD)
  1023. entry = audit_rule_to_entry(data);
  1024. else
  1025. entry = audit_data_to_entry(data, datasz);
  1026. if (IS_ERR(entry))
  1027. return PTR_ERR(entry);
  1028. err = audit_add_rule(entry);
  1029. audit_log_rule_change(loginuid, sessionid, sid, "add",
  1030. &entry->rule, !err);
  1031. if (err)
  1032. audit_free_rule(entry);
  1033. break;
  1034. case AUDIT_DEL:
  1035. case AUDIT_DEL_RULE:
  1036. if (type == AUDIT_DEL)
  1037. entry = audit_rule_to_entry(data);
  1038. else
  1039. entry = audit_data_to_entry(data, datasz);
  1040. if (IS_ERR(entry))
  1041. return PTR_ERR(entry);
  1042. err = audit_del_rule(entry);
  1043. audit_log_rule_change(loginuid, sessionid, sid, "remove",
  1044. &entry->rule, !err);
  1045. audit_free_rule(entry);
  1046. break;
  1047. default:
  1048. return -EINVAL;
  1049. }
  1050. return err;
  1051. }
  1052. int audit_comparator(u32 left, u32 op, u32 right)
  1053. {
  1054. switch (op) {
  1055. case Audit_equal:
  1056. return (left == right);
  1057. case Audit_not_equal:
  1058. return (left != right);
  1059. case Audit_lt:
  1060. return (left < right);
  1061. case Audit_le:
  1062. return (left <= right);
  1063. case Audit_gt:
  1064. return (left > right);
  1065. case Audit_ge:
  1066. return (left >= right);
  1067. case Audit_bitmask:
  1068. return (left & right);
  1069. case Audit_bittest:
  1070. return ((left & right) == right);
  1071. default:
  1072. BUG();
  1073. return 0;
  1074. }
  1075. }
  1076. /* Compare given dentry name with last component in given path,
  1077. * return of 0 indicates a match. */
  1078. int audit_compare_dname_path(const char *dname, const char *path,
  1079. int *dirlen)
  1080. {
  1081. int dlen, plen;
  1082. const char *p;
  1083. if (!dname || !path)
  1084. return 1;
  1085. dlen = strlen(dname);
  1086. plen = strlen(path);
  1087. if (plen < dlen)
  1088. return 1;
  1089. /* disregard trailing slashes */
  1090. p = path + plen - 1;
  1091. while ((*p == '/') && (p > path))
  1092. p--;
  1093. /* find last path component */
  1094. p = p - dlen + 1;
  1095. if (p < path)
  1096. return 1;
  1097. else if (p > path) {
  1098. if (*--p != '/')
  1099. return 1;
  1100. else
  1101. p++;
  1102. }
  1103. /* return length of path's directory component */
  1104. if (dirlen)
  1105. *dirlen = p - path;
  1106. return strncmp(p, dname, dlen);
  1107. }
  1108. static int audit_filter_user_rules(struct netlink_skb_parms *cb,
  1109. struct audit_krule *rule,
  1110. enum audit_state *state)
  1111. {
  1112. int i;
  1113. for (i = 0; i < rule->field_count; i++) {
  1114. struct audit_field *f = &rule->fields[i];
  1115. int result = 0;
  1116. switch (f->type) {
  1117. case AUDIT_PID:
  1118. result = audit_comparator(cb->creds.pid, f->op, f->val);
  1119. break;
  1120. case AUDIT_UID:
  1121. result = audit_comparator(cb->creds.uid, f->op, f->val);
  1122. break;
  1123. case AUDIT_GID:
  1124. result = audit_comparator(cb->creds.gid, f->op, f->val);
  1125. break;
  1126. case AUDIT_LOGINUID:
  1127. result = audit_comparator(cb->loginuid, f->op, f->val);
  1128. break;
  1129. }
  1130. if (!result)
  1131. return 0;
  1132. }
  1133. switch (rule->action) {
  1134. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  1135. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  1136. }
  1137. return 1;
  1138. }
  1139. int audit_filter_user(struct netlink_skb_parms *cb)
  1140. {
  1141. enum audit_state state = AUDIT_DISABLED;
  1142. struct audit_entry *e;
  1143. int ret = 1;
  1144. rcu_read_lock();
  1145. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  1146. if (audit_filter_user_rules(cb, &e->rule, &state)) {
  1147. if (state == AUDIT_DISABLED)
  1148. ret = 0;
  1149. break;
  1150. }
  1151. }
  1152. rcu_read_unlock();
  1153. return ret; /* Audit by default */
  1154. }
  1155. int audit_filter_type(int type)
  1156. {
  1157. struct audit_entry *e;
  1158. int result = 0;
  1159. rcu_read_lock();
  1160. if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
  1161. goto unlock_and_return;
  1162. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
  1163. list) {
  1164. int i;
  1165. for (i = 0; i < e->rule.field_count; i++) {
  1166. struct audit_field *f = &e->rule.fields[i];
  1167. if (f->type == AUDIT_MSGTYPE) {
  1168. result = audit_comparator(type, f->op, f->val);
  1169. if (!result)
  1170. break;
  1171. }
  1172. }
  1173. if (result)
  1174. goto unlock_and_return;
  1175. }
  1176. unlock_and_return:
  1177. rcu_read_unlock();
  1178. return result;
  1179. }
  1180. static int update_lsm_rule(struct audit_krule *r)
  1181. {
  1182. struct audit_entry *entry = container_of(r, struct audit_entry, rule);
  1183. struct audit_entry *nentry;
  1184. struct audit_watch *watch;
  1185. struct audit_tree *tree;
  1186. int err = 0;
  1187. if (!security_audit_rule_known(r))
  1188. return 0;
  1189. watch = r->watch;
  1190. tree = r->tree;
  1191. nentry = audit_dupe_rule(r, watch);
  1192. if (IS_ERR(nentry)) {
  1193. /* save the first error encountered for the
  1194. * return value */
  1195. err = PTR_ERR(nentry);
  1196. audit_panic("error updating LSM filters");
  1197. if (watch)
  1198. list_del(&r->rlist);
  1199. list_del_rcu(&entry->list);
  1200. list_del(&r->list);
  1201. } else {
  1202. if (watch) {
  1203. list_add(&nentry->rule.rlist, audit_watch_rules(watch));
  1204. list_del(&r->rlist);
  1205. } else if (tree)
  1206. list_replace_init(&r->rlist, &nentry->rule.rlist);
  1207. list_replace_rcu(&entry->list, &nentry->list);
  1208. list_replace(&r->list, &nentry->rule.list);
  1209. }
  1210. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1211. return err;
  1212. }
  1213. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1214. * It will traverse the filter lists serarching for rules that contain LSM
  1215. * specific filter fields. When such a rule is found, it is copied, the
  1216. * LSM field is re-initialized, and the old rule is replaced with the
  1217. * updated rule. */
  1218. int audit_update_lsm_rules(void)
  1219. {
  1220. struct audit_krule *r, *n;
  1221. int i, err = 0;
  1222. /* audit_filter_mutex synchronizes the writers */
  1223. mutex_lock(&audit_filter_mutex);
  1224. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1225. list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
  1226. int res = update_lsm_rule(r);
  1227. if (!err)
  1228. err = res;
  1229. }
  1230. }
  1231. mutex_unlock(&audit_filter_mutex);
  1232. return err;
  1233. }