auditfilter.c 34 KB

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