auditfilter.c 33 KB

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