auditfilter.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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 representation. */
  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. case AUDIT_SESSIONID:
  325. /* bit ops are only useful on syscall args */
  326. if (f->op == Audit_bitmask || f->op == Audit_bittest)
  327. return -EINVAL;
  328. break;
  329. case AUDIT_ARG0:
  330. case AUDIT_ARG1:
  331. case AUDIT_ARG2:
  332. case AUDIT_ARG3:
  333. case AUDIT_SUBJ_USER:
  334. case AUDIT_SUBJ_ROLE:
  335. case AUDIT_SUBJ_TYPE:
  336. case AUDIT_SUBJ_SEN:
  337. case AUDIT_SUBJ_CLR:
  338. case AUDIT_OBJ_USER:
  339. case AUDIT_OBJ_ROLE:
  340. case AUDIT_OBJ_TYPE:
  341. case AUDIT_OBJ_LEV_LOW:
  342. case AUDIT_OBJ_LEV_HIGH:
  343. case AUDIT_WATCH:
  344. case AUDIT_DIR:
  345. case AUDIT_FILTERKEY:
  346. break;
  347. case AUDIT_LOGINUID_SET:
  348. if ((f->val != 0) && (f->val != 1))
  349. return -EINVAL;
  350. /* FALL THROUGH */
  351. case AUDIT_ARCH:
  352. if (f->op != Audit_not_equal && f->op != Audit_equal)
  353. return -EINVAL;
  354. break;
  355. case AUDIT_PERM:
  356. if (f->val & ~15)
  357. return -EINVAL;
  358. break;
  359. case AUDIT_FILETYPE:
  360. if (f->val & ~S_IFMT)
  361. return -EINVAL;
  362. break;
  363. case AUDIT_FIELD_COMPARE:
  364. if (f->val > AUDIT_MAX_FIELD_COMPARE)
  365. return -EINVAL;
  366. break;
  367. case AUDIT_EXE:
  368. if (f->op != Audit_equal)
  369. return -EINVAL;
  370. if (entry->rule.listnr != AUDIT_FILTER_EXIT)
  371. return -EINVAL;
  372. break;
  373. }
  374. return 0;
  375. }
  376. /* Translate struct audit_rule_data to kernel's rule representation. */
  377. static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
  378. size_t datasz)
  379. {
  380. int err = 0;
  381. struct audit_entry *entry;
  382. void *bufp;
  383. size_t remain = datasz - sizeof(struct audit_rule_data);
  384. int i;
  385. char *str;
  386. struct audit_fsnotify_mark *audit_mark;
  387. entry = audit_to_entry_common(data);
  388. if (IS_ERR(entry))
  389. goto exit_nofree;
  390. bufp = data->buf;
  391. for (i = 0; i < data->field_count; i++) {
  392. struct audit_field *f = &entry->rule.fields[i];
  393. err = -EINVAL;
  394. f->op = audit_to_op(data->fieldflags[i]);
  395. if (f->op == Audit_bad)
  396. goto exit_free;
  397. f->type = data->fields[i];
  398. f->val = data->values[i];
  399. /* Support legacy tests for a valid loginuid */
  400. if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) {
  401. f->type = AUDIT_LOGINUID_SET;
  402. f->val = 0;
  403. entry->rule.pflags |= AUDIT_LOGINUID_LEGACY;
  404. }
  405. err = audit_field_valid(entry, f);
  406. if (err)
  407. goto exit_free;
  408. err = -EINVAL;
  409. switch (f->type) {
  410. case AUDIT_LOGINUID:
  411. case AUDIT_UID:
  412. case AUDIT_EUID:
  413. case AUDIT_SUID:
  414. case AUDIT_FSUID:
  415. case AUDIT_OBJ_UID:
  416. f->uid = make_kuid(current_user_ns(), f->val);
  417. if (!uid_valid(f->uid))
  418. goto exit_free;
  419. break;
  420. case AUDIT_GID:
  421. case AUDIT_EGID:
  422. case AUDIT_SGID:
  423. case AUDIT_FSGID:
  424. case AUDIT_OBJ_GID:
  425. f->gid = make_kgid(current_user_ns(), f->val);
  426. if (!gid_valid(f->gid))
  427. goto exit_free;
  428. break;
  429. case AUDIT_SESSIONID:
  430. case AUDIT_ARCH:
  431. entry->rule.arch_f = f;
  432. break;
  433. case AUDIT_SUBJ_USER:
  434. case AUDIT_SUBJ_ROLE:
  435. case AUDIT_SUBJ_TYPE:
  436. case AUDIT_SUBJ_SEN:
  437. case AUDIT_SUBJ_CLR:
  438. case AUDIT_OBJ_USER:
  439. case AUDIT_OBJ_ROLE:
  440. case AUDIT_OBJ_TYPE:
  441. case AUDIT_OBJ_LEV_LOW:
  442. case AUDIT_OBJ_LEV_HIGH:
  443. str = audit_unpack_string(&bufp, &remain, f->val);
  444. if (IS_ERR(str))
  445. goto exit_free;
  446. entry->rule.buflen += f->val;
  447. err = security_audit_rule_init(f->type, f->op, str,
  448. (void **)&f->lsm_rule);
  449. /* Keep currently invalid fields around in case they
  450. * become valid after a policy reload. */
  451. if (err == -EINVAL) {
  452. pr_warn("audit rule for LSM \'%s\' is invalid\n",
  453. str);
  454. err = 0;
  455. }
  456. if (err) {
  457. kfree(str);
  458. goto exit_free;
  459. } else
  460. f->lsm_str = str;
  461. break;
  462. case AUDIT_WATCH:
  463. str = audit_unpack_string(&bufp, &remain, f->val);
  464. if (IS_ERR(str))
  465. goto exit_free;
  466. entry->rule.buflen += f->val;
  467. err = audit_to_watch(&entry->rule, str, f->val, f->op);
  468. if (err) {
  469. kfree(str);
  470. goto exit_free;
  471. }
  472. break;
  473. case AUDIT_DIR:
  474. str = audit_unpack_string(&bufp, &remain, f->val);
  475. if (IS_ERR(str))
  476. goto exit_free;
  477. entry->rule.buflen += f->val;
  478. err = audit_make_tree(&entry->rule, str, f->op);
  479. kfree(str);
  480. if (err)
  481. goto exit_free;
  482. break;
  483. case AUDIT_INODE:
  484. err = audit_to_inode(&entry->rule, f);
  485. if (err)
  486. goto exit_free;
  487. break;
  488. case AUDIT_FILTERKEY:
  489. if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
  490. goto exit_free;
  491. str = audit_unpack_string(&bufp, &remain, f->val);
  492. if (IS_ERR(str))
  493. goto exit_free;
  494. entry->rule.buflen += f->val;
  495. entry->rule.filterkey = str;
  496. break;
  497. case AUDIT_EXE:
  498. if (entry->rule.exe || f->val > PATH_MAX)
  499. goto exit_free;
  500. str = audit_unpack_string(&bufp, &remain, f->val);
  501. if (IS_ERR(str)) {
  502. err = PTR_ERR(str);
  503. goto exit_free;
  504. }
  505. entry->rule.buflen += f->val;
  506. audit_mark = audit_alloc_mark(&entry->rule, str, f->val);
  507. if (IS_ERR(audit_mark)) {
  508. kfree(str);
  509. err = PTR_ERR(audit_mark);
  510. goto exit_free;
  511. }
  512. entry->rule.exe = audit_mark;
  513. break;
  514. }
  515. }
  516. if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
  517. entry->rule.inode_f = NULL;
  518. exit_nofree:
  519. return entry;
  520. exit_free:
  521. if (entry->rule.tree)
  522. audit_put_tree(entry->rule.tree); /* that's the temporary one */
  523. if (entry->rule.exe)
  524. audit_remove_mark(entry->rule.exe); /* that's the template one */
  525. audit_free_rule(entry);
  526. return ERR_PTR(err);
  527. }
  528. /* Pack a filter field's string representation into data block. */
  529. static inline size_t audit_pack_string(void **bufp, const char *str)
  530. {
  531. size_t len = strlen(str);
  532. memcpy(*bufp, str, len);
  533. *bufp += len;
  534. return len;
  535. }
  536. /* Translate kernel rule representation to struct audit_rule_data. */
  537. static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
  538. {
  539. struct audit_rule_data *data;
  540. void *bufp;
  541. int i;
  542. data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
  543. if (unlikely(!data))
  544. return NULL;
  545. memset(data, 0, sizeof(*data));
  546. data->flags = krule->flags | krule->listnr;
  547. data->action = krule->action;
  548. data->field_count = krule->field_count;
  549. bufp = data->buf;
  550. for (i = 0; i < data->field_count; i++) {
  551. struct audit_field *f = &krule->fields[i];
  552. data->fields[i] = f->type;
  553. data->fieldflags[i] = audit_ops[f->op];
  554. switch(f->type) {
  555. case AUDIT_SUBJ_USER:
  556. case AUDIT_SUBJ_ROLE:
  557. case AUDIT_SUBJ_TYPE:
  558. case AUDIT_SUBJ_SEN:
  559. case AUDIT_SUBJ_CLR:
  560. case AUDIT_OBJ_USER:
  561. case AUDIT_OBJ_ROLE:
  562. case AUDIT_OBJ_TYPE:
  563. case AUDIT_OBJ_LEV_LOW:
  564. case AUDIT_OBJ_LEV_HIGH:
  565. data->buflen += data->values[i] =
  566. audit_pack_string(&bufp, f->lsm_str);
  567. break;
  568. case AUDIT_WATCH:
  569. data->buflen += data->values[i] =
  570. audit_pack_string(&bufp,
  571. audit_watch_path(krule->watch));
  572. break;
  573. case AUDIT_DIR:
  574. data->buflen += data->values[i] =
  575. audit_pack_string(&bufp,
  576. audit_tree_path(krule->tree));
  577. break;
  578. case AUDIT_FILTERKEY:
  579. data->buflen += data->values[i] =
  580. audit_pack_string(&bufp, krule->filterkey);
  581. break;
  582. case AUDIT_EXE:
  583. data->buflen += data->values[i] =
  584. audit_pack_string(&bufp, audit_mark_path(krule->exe));
  585. break;
  586. case AUDIT_LOGINUID_SET:
  587. if (krule->pflags & AUDIT_LOGINUID_LEGACY && !f->val) {
  588. data->fields[i] = AUDIT_LOGINUID;
  589. data->values[i] = AUDIT_UID_UNSET;
  590. break;
  591. }
  592. /* fallthrough if set */
  593. default:
  594. data->values[i] = f->val;
  595. }
  596. }
  597. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
  598. return data;
  599. }
  600. /* Compare two rules in kernel format. Considered success if rules
  601. * don't match. */
  602. static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
  603. {
  604. int i;
  605. if (a->flags != b->flags ||
  606. a->pflags != b->pflags ||
  607. a->listnr != b->listnr ||
  608. a->action != b->action ||
  609. a->field_count != b->field_count)
  610. return 1;
  611. for (i = 0; i < a->field_count; i++) {
  612. if (a->fields[i].type != b->fields[i].type ||
  613. a->fields[i].op != b->fields[i].op)
  614. return 1;
  615. switch(a->fields[i].type) {
  616. case AUDIT_SUBJ_USER:
  617. case AUDIT_SUBJ_ROLE:
  618. case AUDIT_SUBJ_TYPE:
  619. case AUDIT_SUBJ_SEN:
  620. case AUDIT_SUBJ_CLR:
  621. case AUDIT_OBJ_USER:
  622. case AUDIT_OBJ_ROLE:
  623. case AUDIT_OBJ_TYPE:
  624. case AUDIT_OBJ_LEV_LOW:
  625. case AUDIT_OBJ_LEV_HIGH:
  626. if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
  627. return 1;
  628. break;
  629. case AUDIT_WATCH:
  630. if (strcmp(audit_watch_path(a->watch),
  631. audit_watch_path(b->watch)))
  632. return 1;
  633. break;
  634. case AUDIT_DIR:
  635. if (strcmp(audit_tree_path(a->tree),
  636. audit_tree_path(b->tree)))
  637. return 1;
  638. break;
  639. case AUDIT_FILTERKEY:
  640. /* both filterkeys exist based on above type compare */
  641. if (strcmp(a->filterkey, b->filterkey))
  642. return 1;
  643. break;
  644. case AUDIT_EXE:
  645. /* both paths exist based on above type compare */
  646. if (strcmp(audit_mark_path(a->exe),
  647. audit_mark_path(b->exe)))
  648. return 1;
  649. break;
  650. case AUDIT_UID:
  651. case AUDIT_EUID:
  652. case AUDIT_SUID:
  653. case AUDIT_FSUID:
  654. case AUDIT_LOGINUID:
  655. case AUDIT_OBJ_UID:
  656. if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
  657. return 1;
  658. break;
  659. case AUDIT_GID:
  660. case AUDIT_EGID:
  661. case AUDIT_SGID:
  662. case AUDIT_FSGID:
  663. case AUDIT_OBJ_GID:
  664. if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
  665. return 1;
  666. break;
  667. default:
  668. if (a->fields[i].val != b->fields[i].val)
  669. return 1;
  670. }
  671. }
  672. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  673. if (a->mask[i] != b->mask[i])
  674. return 1;
  675. return 0;
  676. }
  677. /* Duplicate LSM field information. The lsm_rule is opaque, so must be
  678. * re-initialized. */
  679. static inline int audit_dupe_lsm_field(struct audit_field *df,
  680. struct audit_field *sf)
  681. {
  682. int ret = 0;
  683. char *lsm_str;
  684. /* our own copy of lsm_str */
  685. lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
  686. if (unlikely(!lsm_str))
  687. return -ENOMEM;
  688. df->lsm_str = lsm_str;
  689. /* our own (refreshed) copy of lsm_rule */
  690. ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
  691. (void **)&df->lsm_rule);
  692. /* Keep currently invalid fields around in case they
  693. * become valid after a policy reload. */
  694. if (ret == -EINVAL) {
  695. pr_warn("audit rule for LSM \'%s\' is invalid\n",
  696. df->lsm_str);
  697. ret = 0;
  698. }
  699. return ret;
  700. }
  701. /* Duplicate an audit rule. This will be a deep copy with the exception
  702. * of the watch - that pointer is carried over. The LSM specific fields
  703. * will be updated in the copy. The point is to be able to replace the old
  704. * rule with the new rule in the filterlist, then free the old rule.
  705. * The rlist element is undefined; list manipulations are handled apart from
  706. * the initial copy. */
  707. struct audit_entry *audit_dupe_rule(struct audit_krule *old)
  708. {
  709. u32 fcount = old->field_count;
  710. struct audit_entry *entry;
  711. struct audit_krule *new;
  712. char *fk;
  713. int i, err = 0;
  714. entry = audit_init_entry(fcount);
  715. if (unlikely(!entry))
  716. return ERR_PTR(-ENOMEM);
  717. new = &entry->rule;
  718. new->flags = old->flags;
  719. new->pflags = old->pflags;
  720. new->listnr = old->listnr;
  721. new->action = old->action;
  722. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  723. new->mask[i] = old->mask[i];
  724. new->prio = old->prio;
  725. new->buflen = old->buflen;
  726. new->inode_f = old->inode_f;
  727. new->field_count = old->field_count;
  728. /*
  729. * note that we are OK with not refcounting here; audit_match_tree()
  730. * never dereferences tree and we can't get false positives there
  731. * since we'd have to have rule gone from the list *and* removed
  732. * before the chunks found by lookup had been allocated, i.e. before
  733. * the beginning of list scan.
  734. */
  735. new->tree = old->tree;
  736. memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
  737. /* deep copy this information, updating the lsm_rule fields, because
  738. * the originals will all be freed when the old rule is freed. */
  739. for (i = 0; i < fcount; i++) {
  740. switch (new->fields[i].type) {
  741. case AUDIT_SUBJ_USER:
  742. case AUDIT_SUBJ_ROLE:
  743. case AUDIT_SUBJ_TYPE:
  744. case AUDIT_SUBJ_SEN:
  745. case AUDIT_SUBJ_CLR:
  746. case AUDIT_OBJ_USER:
  747. case AUDIT_OBJ_ROLE:
  748. case AUDIT_OBJ_TYPE:
  749. case AUDIT_OBJ_LEV_LOW:
  750. case AUDIT_OBJ_LEV_HIGH:
  751. err = audit_dupe_lsm_field(&new->fields[i],
  752. &old->fields[i]);
  753. break;
  754. case AUDIT_FILTERKEY:
  755. fk = kstrdup(old->filterkey, GFP_KERNEL);
  756. if (unlikely(!fk))
  757. err = -ENOMEM;
  758. else
  759. new->filterkey = fk;
  760. break;
  761. case AUDIT_EXE:
  762. err = audit_dupe_exe(new, old);
  763. break;
  764. }
  765. if (err) {
  766. if (new->exe)
  767. audit_remove_mark(new->exe);
  768. audit_free_rule(entry);
  769. return ERR_PTR(err);
  770. }
  771. }
  772. if (old->watch) {
  773. audit_get_watch(old->watch);
  774. new->watch = old->watch;
  775. }
  776. return entry;
  777. }
  778. /* Find an existing audit rule.
  779. * Caller must hold audit_filter_mutex to prevent stale rule data. */
  780. static struct audit_entry *audit_find_rule(struct audit_entry *entry,
  781. struct list_head **p)
  782. {
  783. struct audit_entry *e, *found = NULL;
  784. struct list_head *list;
  785. int h;
  786. if (entry->rule.inode_f) {
  787. h = audit_hash_ino(entry->rule.inode_f->val);
  788. *p = list = &audit_inode_hash[h];
  789. } else if (entry->rule.watch) {
  790. /* we don't know the inode number, so must walk entire hash */
  791. for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
  792. list = &audit_inode_hash[h];
  793. list_for_each_entry(e, list, list)
  794. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  795. found = e;
  796. goto out;
  797. }
  798. }
  799. goto out;
  800. } else {
  801. *p = list = &audit_filter_list[entry->rule.listnr];
  802. }
  803. list_for_each_entry(e, list, list)
  804. if (!audit_compare_rule(&entry->rule, &e->rule)) {
  805. found = e;
  806. goto out;
  807. }
  808. out:
  809. return found;
  810. }
  811. static u64 prio_low = ~0ULL/2;
  812. static u64 prio_high = ~0ULL/2 - 1;
  813. /* Add rule to given filterlist if not a duplicate. */
  814. static inline int audit_add_rule(struct audit_entry *entry)
  815. {
  816. struct audit_entry *e;
  817. struct audit_watch *watch = entry->rule.watch;
  818. struct audit_tree *tree = entry->rule.tree;
  819. struct list_head *list;
  820. int err = 0;
  821. #ifdef CONFIG_AUDITSYSCALL
  822. int dont_count = 0;
  823. /* If either of these, don't count towards total */
  824. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  825. entry->rule.listnr == AUDIT_FILTER_TYPE)
  826. dont_count = 1;
  827. #endif
  828. mutex_lock(&audit_filter_mutex);
  829. e = audit_find_rule(entry, &list);
  830. if (e) {
  831. mutex_unlock(&audit_filter_mutex);
  832. err = -EEXIST;
  833. /* normally audit_add_tree_rule() will free it on failure */
  834. if (tree)
  835. audit_put_tree(tree);
  836. return err;
  837. }
  838. if (watch) {
  839. /* audit_filter_mutex is dropped and re-taken during this call */
  840. err = audit_add_watch(&entry->rule, &list);
  841. if (err) {
  842. mutex_unlock(&audit_filter_mutex);
  843. /*
  844. * normally audit_add_tree_rule() will free it
  845. * on failure
  846. */
  847. if (tree)
  848. audit_put_tree(tree);
  849. return err;
  850. }
  851. }
  852. if (tree) {
  853. err = audit_add_tree_rule(&entry->rule);
  854. if (err) {
  855. mutex_unlock(&audit_filter_mutex);
  856. return err;
  857. }
  858. }
  859. entry->rule.prio = ~0ULL;
  860. if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
  861. if (entry->rule.flags & AUDIT_FILTER_PREPEND)
  862. entry->rule.prio = ++prio_high;
  863. else
  864. entry->rule.prio = --prio_low;
  865. }
  866. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  867. list_add(&entry->rule.list,
  868. &audit_rules_list[entry->rule.listnr]);
  869. list_add_rcu(&entry->list, list);
  870. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  871. } else {
  872. list_add_tail(&entry->rule.list,
  873. &audit_rules_list[entry->rule.listnr]);
  874. list_add_tail_rcu(&entry->list, list);
  875. }
  876. #ifdef CONFIG_AUDITSYSCALL
  877. if (!dont_count)
  878. audit_n_rules++;
  879. if (!audit_match_signal(entry))
  880. audit_signals++;
  881. #endif
  882. mutex_unlock(&audit_filter_mutex);
  883. return err;
  884. }
  885. /* Remove an existing rule from filterlist. */
  886. int audit_del_rule(struct audit_entry *entry)
  887. {
  888. struct audit_entry *e;
  889. struct audit_tree *tree = entry->rule.tree;
  890. struct list_head *list;
  891. int ret = 0;
  892. #ifdef CONFIG_AUDITSYSCALL
  893. int dont_count = 0;
  894. /* If either of these, don't count towards total */
  895. if (entry->rule.listnr == AUDIT_FILTER_USER ||
  896. entry->rule.listnr == AUDIT_FILTER_TYPE)
  897. dont_count = 1;
  898. #endif
  899. mutex_lock(&audit_filter_mutex);
  900. e = audit_find_rule(entry, &list);
  901. if (!e) {
  902. ret = -ENOENT;
  903. goto out;
  904. }
  905. if (e->rule.watch)
  906. audit_remove_watch_rule(&e->rule);
  907. if (e->rule.tree)
  908. audit_remove_tree_rule(&e->rule);
  909. if (e->rule.exe)
  910. audit_remove_mark_rule(&e->rule);
  911. #ifdef CONFIG_AUDITSYSCALL
  912. if (!dont_count)
  913. audit_n_rules--;
  914. if (!audit_match_signal(entry))
  915. audit_signals--;
  916. #endif
  917. list_del_rcu(&e->list);
  918. list_del(&e->rule.list);
  919. call_rcu(&e->rcu, audit_free_rule_rcu);
  920. out:
  921. mutex_unlock(&audit_filter_mutex);
  922. if (tree)
  923. audit_put_tree(tree); /* that's the temporary one */
  924. return ret;
  925. }
  926. /* List rules using struct audit_rule_data. */
  927. static void audit_list_rules(int seq, struct sk_buff_head *q)
  928. {
  929. struct sk_buff *skb;
  930. struct audit_krule *r;
  931. int i;
  932. /* This is a blocking read, so use audit_filter_mutex instead of rcu
  933. * iterator to sync with list writers. */
  934. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  935. list_for_each_entry(r, &audit_rules_list[i], list) {
  936. struct audit_rule_data *data;
  937. data = audit_krule_to_data(r);
  938. if (unlikely(!data))
  939. break;
  940. skb = audit_make_reply(seq, AUDIT_LIST_RULES, 0, 1,
  941. data,
  942. sizeof(*data) + data->buflen);
  943. if (skb)
  944. skb_queue_tail(q, skb);
  945. kfree(data);
  946. }
  947. }
  948. skb = audit_make_reply(seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
  949. if (skb)
  950. skb_queue_tail(q, skb);
  951. }
  952. /* Log rule additions and removals */
  953. static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
  954. {
  955. struct audit_buffer *ab;
  956. uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
  957. unsigned int sessionid = audit_get_sessionid(current);
  958. if (!audit_enabled)
  959. return;
  960. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
  961. if (!ab)
  962. return;
  963. audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid);
  964. audit_log_task_context(ab);
  965. audit_log_format(ab, " op=%s", action);
  966. audit_log_key(ab, rule->filterkey);
  967. audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
  968. audit_log_end(ab);
  969. }
  970. /**
  971. * audit_rule_change - apply all rules to the specified message type
  972. * @type: audit message type
  973. * @seq: netlink audit message sequence (serial) number
  974. * @data: payload data
  975. * @datasz: size of payload data
  976. */
  977. int audit_rule_change(int type, int seq, void *data, size_t datasz)
  978. {
  979. int err = 0;
  980. struct audit_entry *entry;
  981. entry = audit_data_to_entry(data, datasz);
  982. if (IS_ERR(entry))
  983. return PTR_ERR(entry);
  984. switch (type) {
  985. case AUDIT_ADD_RULE:
  986. err = audit_add_rule(entry);
  987. audit_log_rule_change("add_rule", &entry->rule, !err);
  988. break;
  989. case AUDIT_DEL_RULE:
  990. err = audit_del_rule(entry);
  991. audit_log_rule_change("remove_rule", &entry->rule, !err);
  992. break;
  993. default:
  994. err = -EINVAL;
  995. WARN_ON(1);
  996. }
  997. if (err || type == AUDIT_DEL_RULE) {
  998. if (entry->rule.exe)
  999. audit_remove_mark(entry->rule.exe);
  1000. audit_free_rule(entry);
  1001. }
  1002. return err;
  1003. }
  1004. /**
  1005. * audit_list_rules_send - list the audit rules
  1006. * @request_skb: skb of request we are replying to (used to target the reply)
  1007. * @seq: netlink audit message sequence (serial) number
  1008. */
  1009. int audit_list_rules_send(struct sk_buff *request_skb, int seq)
  1010. {
  1011. u32 portid = NETLINK_CB(request_skb).portid;
  1012. struct net *net = sock_net(NETLINK_CB(request_skb).sk);
  1013. struct task_struct *tsk;
  1014. struct audit_netlink_list *dest;
  1015. int err = 0;
  1016. /* We can't just spew out the rules here because we might fill
  1017. * the available socket buffer space and deadlock waiting for
  1018. * auditctl to read from it... which isn't ever going to
  1019. * happen if we're actually running in the context of auditctl
  1020. * trying to _send_ the stuff */
  1021. dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
  1022. if (!dest)
  1023. return -ENOMEM;
  1024. dest->net = get_net(net);
  1025. dest->portid = portid;
  1026. skb_queue_head_init(&dest->q);
  1027. mutex_lock(&audit_filter_mutex);
  1028. audit_list_rules(seq, &dest->q);
  1029. mutex_unlock(&audit_filter_mutex);
  1030. tsk = kthread_run(audit_send_list, dest, "audit_send_list");
  1031. if (IS_ERR(tsk)) {
  1032. skb_queue_purge(&dest->q);
  1033. kfree(dest);
  1034. err = PTR_ERR(tsk);
  1035. }
  1036. return err;
  1037. }
  1038. int audit_comparator(u32 left, u32 op, u32 right)
  1039. {
  1040. switch (op) {
  1041. case Audit_equal:
  1042. return (left == right);
  1043. case Audit_not_equal:
  1044. return (left != right);
  1045. case Audit_lt:
  1046. return (left < right);
  1047. case Audit_le:
  1048. return (left <= right);
  1049. case Audit_gt:
  1050. return (left > right);
  1051. case Audit_ge:
  1052. return (left >= right);
  1053. case Audit_bitmask:
  1054. return (left & right);
  1055. case Audit_bittest:
  1056. return ((left & right) == right);
  1057. default:
  1058. BUG();
  1059. return 0;
  1060. }
  1061. }
  1062. int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
  1063. {
  1064. switch (op) {
  1065. case Audit_equal:
  1066. return uid_eq(left, right);
  1067. case Audit_not_equal:
  1068. return !uid_eq(left, right);
  1069. case Audit_lt:
  1070. return uid_lt(left, right);
  1071. case Audit_le:
  1072. return uid_lte(left, right);
  1073. case Audit_gt:
  1074. return uid_gt(left, right);
  1075. case Audit_ge:
  1076. return uid_gte(left, right);
  1077. case Audit_bitmask:
  1078. case Audit_bittest:
  1079. default:
  1080. BUG();
  1081. return 0;
  1082. }
  1083. }
  1084. int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
  1085. {
  1086. switch (op) {
  1087. case Audit_equal:
  1088. return gid_eq(left, right);
  1089. case Audit_not_equal:
  1090. return !gid_eq(left, right);
  1091. case Audit_lt:
  1092. return gid_lt(left, right);
  1093. case Audit_le:
  1094. return gid_lte(left, right);
  1095. case Audit_gt:
  1096. return gid_gt(left, right);
  1097. case Audit_ge:
  1098. return gid_gte(left, right);
  1099. case Audit_bitmask:
  1100. case Audit_bittest:
  1101. default:
  1102. BUG();
  1103. return 0;
  1104. }
  1105. }
  1106. /**
  1107. * parent_len - find the length of the parent portion of a pathname
  1108. * @path: pathname of which to determine length
  1109. */
  1110. int parent_len(const char *path)
  1111. {
  1112. int plen;
  1113. const char *p;
  1114. plen = strlen(path);
  1115. if (plen == 0)
  1116. return plen;
  1117. /* disregard trailing slashes */
  1118. p = path + plen - 1;
  1119. while ((*p == '/') && (p > path))
  1120. p--;
  1121. /* walk backward until we find the next slash or hit beginning */
  1122. while ((*p != '/') && (p > path))
  1123. p--;
  1124. /* did we find a slash? Then increment to include it in path */
  1125. if (*p == '/')
  1126. p++;
  1127. return p - path;
  1128. }
  1129. /**
  1130. * audit_compare_dname_path - compare given dentry name with last component in
  1131. * given path. Return of 0 indicates a match.
  1132. * @dname: dentry name that we're comparing
  1133. * @path: full pathname that we're comparing
  1134. * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
  1135. * here indicates that we must compute this value.
  1136. */
  1137. int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
  1138. {
  1139. int dlen, pathlen;
  1140. const char *p;
  1141. dlen = strlen(dname);
  1142. pathlen = strlen(path);
  1143. if (pathlen < dlen)
  1144. return 1;
  1145. parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
  1146. if (pathlen - parentlen != dlen)
  1147. return 1;
  1148. p = path + parentlen;
  1149. return strncmp(p, dname, dlen);
  1150. }
  1151. int audit_filter(int msgtype, unsigned int listtype)
  1152. {
  1153. struct audit_entry *e;
  1154. int ret = 1; /* Audit by default */
  1155. rcu_read_lock();
  1156. if (list_empty(&audit_filter_list[listtype]))
  1157. goto unlock_and_return;
  1158. list_for_each_entry_rcu(e, &audit_filter_list[listtype], list) {
  1159. int i, result = 0;
  1160. for (i = 0; i < e->rule.field_count; i++) {
  1161. struct audit_field *f = &e->rule.fields[i];
  1162. pid_t pid;
  1163. u32 sid;
  1164. switch (f->type) {
  1165. case AUDIT_PID:
  1166. pid = task_pid_nr(current);
  1167. result = audit_comparator(pid, f->op, f->val);
  1168. break;
  1169. case AUDIT_UID:
  1170. result = audit_uid_comparator(current_uid(), f->op, f->uid);
  1171. break;
  1172. case AUDIT_GID:
  1173. result = audit_gid_comparator(current_gid(), f->op, f->gid);
  1174. break;
  1175. case AUDIT_LOGINUID:
  1176. result = audit_uid_comparator(audit_get_loginuid(current),
  1177. f->op, f->uid);
  1178. break;
  1179. case AUDIT_LOGINUID_SET:
  1180. result = audit_comparator(audit_loginuid_set(current),
  1181. f->op, f->val);
  1182. break;
  1183. case AUDIT_MSGTYPE:
  1184. result = audit_comparator(msgtype, f->op, f->val);
  1185. break;
  1186. case AUDIT_SUBJ_USER:
  1187. case AUDIT_SUBJ_ROLE:
  1188. case AUDIT_SUBJ_TYPE:
  1189. case AUDIT_SUBJ_SEN:
  1190. case AUDIT_SUBJ_CLR:
  1191. if (f->lsm_rule) {
  1192. security_task_getsecid(current, &sid);
  1193. result = security_audit_rule_match(sid,
  1194. f->type, f->op, f->lsm_rule, NULL);
  1195. }
  1196. break;
  1197. default:
  1198. goto unlock_and_return;
  1199. }
  1200. if (result < 0) /* error */
  1201. goto unlock_and_return;
  1202. if (!result)
  1203. break;
  1204. }
  1205. if (result > 0) {
  1206. if (e->rule.action == AUDIT_NEVER || listtype == AUDIT_FILTER_TYPE)
  1207. ret = 0;
  1208. break;
  1209. }
  1210. }
  1211. unlock_and_return:
  1212. rcu_read_unlock();
  1213. return ret;
  1214. }
  1215. static int update_lsm_rule(struct audit_krule *r)
  1216. {
  1217. struct audit_entry *entry = container_of(r, struct audit_entry, rule);
  1218. struct audit_entry *nentry;
  1219. int err = 0;
  1220. if (!security_audit_rule_known(r))
  1221. return 0;
  1222. nentry = audit_dupe_rule(r);
  1223. if (entry->rule.exe)
  1224. audit_remove_mark(entry->rule.exe);
  1225. if (IS_ERR(nentry)) {
  1226. /* save the first error encountered for the
  1227. * return value */
  1228. err = PTR_ERR(nentry);
  1229. audit_panic("error updating LSM filters");
  1230. if (r->watch)
  1231. list_del(&r->rlist);
  1232. list_del_rcu(&entry->list);
  1233. list_del(&r->list);
  1234. } else {
  1235. if (r->watch || r->tree)
  1236. list_replace_init(&r->rlist, &nentry->rule.rlist);
  1237. list_replace_rcu(&entry->list, &nentry->list);
  1238. list_replace(&r->list, &nentry->rule.list);
  1239. }
  1240. call_rcu(&entry->rcu, audit_free_rule_rcu);
  1241. return err;
  1242. }
  1243. /* This function will re-initialize the lsm_rule field of all applicable rules.
  1244. * It will traverse the filter lists serarching for rules that contain LSM
  1245. * specific filter fields. When such a rule is found, it is copied, the
  1246. * LSM field is re-initialized, and the old rule is replaced with the
  1247. * updated rule. */
  1248. int audit_update_lsm_rules(void)
  1249. {
  1250. struct audit_krule *r, *n;
  1251. int i, err = 0;
  1252. /* audit_filter_mutex synchronizes the writers */
  1253. mutex_lock(&audit_filter_mutex);
  1254. for (i = 0; i < AUDIT_NR_FILTERS; i++) {
  1255. list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
  1256. int res = update_lsm_rule(r);
  1257. if (!err)
  1258. err = res;
  1259. }
  1260. }
  1261. mutex_unlock(&audit_filter_mutex);
  1262. return err;
  1263. }