lsm_audit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * common LSM auditing functions
  4. *
  5. * Based on code written for SELinux by :
  6. * Stephen Smalley, <sds@tycho.nsa.gov>
  7. * James Morris <jmorris@redhat.com>
  8. * Author : Etienne Basset, <etienne.basset@ensta.org>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/stddef.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gfp.h>
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <net/sock.h>
  17. #include <linux/un.h>
  18. #include <net/af_unix.h>
  19. #include <linux/audit.h>
  20. #include <linux/ipv6.h>
  21. #include <linux/ip.h>
  22. #include <net/ip.h>
  23. #include <net/ipv6.h>
  24. #include <linux/tcp.h>
  25. #include <linux/udp.h>
  26. #include <linux/dccp.h>
  27. #include <linux/sctp.h>
  28. #include <linux/lsm_audit.h>
  29. #include <linux/security.h>
  30. /**
  31. * ipv4_skb_to_auditdata : fill auditdata from skb
  32. * @skb : the skb
  33. * @ad : the audit data to fill
  34. * @proto : the layer 4 protocol
  35. *
  36. * return 0 on success
  37. */
  38. int ipv4_skb_to_auditdata(struct sk_buff *skb,
  39. struct common_audit_data *ad, u8 *proto)
  40. {
  41. int ret = 0;
  42. struct iphdr *ih;
  43. ih = ip_hdr(skb);
  44. ad->u.net->v4info.saddr = ih->saddr;
  45. ad->u.net->v4info.daddr = ih->daddr;
  46. if (proto)
  47. *proto = ih->protocol;
  48. /* non initial fragment */
  49. if (ntohs(ih->frag_off) & IP_OFFSET)
  50. return 0;
  51. switch (ih->protocol) {
  52. case IPPROTO_TCP: {
  53. struct tcphdr *th = tcp_hdr(skb);
  54. ad->u.net->sport = th->source;
  55. ad->u.net->dport = th->dest;
  56. break;
  57. }
  58. case IPPROTO_UDP: {
  59. struct udphdr *uh = udp_hdr(skb);
  60. ad->u.net->sport = uh->source;
  61. ad->u.net->dport = uh->dest;
  62. break;
  63. }
  64. case IPPROTO_DCCP: {
  65. struct dccp_hdr *dh = dccp_hdr(skb);
  66. ad->u.net->sport = dh->dccph_sport;
  67. ad->u.net->dport = dh->dccph_dport;
  68. break;
  69. }
  70. case IPPROTO_SCTP: {
  71. struct sctphdr *sh = sctp_hdr(skb);
  72. ad->u.net->sport = sh->source;
  73. ad->u.net->dport = sh->dest;
  74. break;
  75. }
  76. default:
  77. ret = -EINVAL;
  78. }
  79. return ret;
  80. }
  81. #if IS_ENABLED(CONFIG_IPV6)
  82. /**
  83. * ipv6_skb_to_auditdata : fill auditdata from skb
  84. * @skb : the skb
  85. * @ad : the audit data to fill
  86. * @proto : the layer 4 protocol
  87. *
  88. * return 0 on success
  89. */
  90. int ipv6_skb_to_auditdata(struct sk_buff *skb,
  91. struct common_audit_data *ad, u8 *proto)
  92. {
  93. int offset, ret = 0;
  94. struct ipv6hdr *ip6;
  95. u8 nexthdr;
  96. __be16 frag_off;
  97. ip6 = ipv6_hdr(skb);
  98. ad->u.net->v6info.saddr = ip6->saddr;
  99. ad->u.net->v6info.daddr = ip6->daddr;
  100. /* IPv6 can have several extension header before the Transport header
  101. * skip them */
  102. offset = skb_network_offset(skb);
  103. offset += sizeof(*ip6);
  104. nexthdr = ip6->nexthdr;
  105. offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
  106. if (offset < 0)
  107. return 0;
  108. if (proto)
  109. *proto = nexthdr;
  110. switch (nexthdr) {
  111. case IPPROTO_TCP: {
  112. struct tcphdr _tcph, *th;
  113. th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
  114. if (th == NULL)
  115. break;
  116. ad->u.net->sport = th->source;
  117. ad->u.net->dport = th->dest;
  118. break;
  119. }
  120. case IPPROTO_UDP: {
  121. struct udphdr _udph, *uh;
  122. uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
  123. if (uh == NULL)
  124. break;
  125. ad->u.net->sport = uh->source;
  126. ad->u.net->dport = uh->dest;
  127. break;
  128. }
  129. case IPPROTO_DCCP: {
  130. struct dccp_hdr _dccph, *dh;
  131. dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
  132. if (dh == NULL)
  133. break;
  134. ad->u.net->sport = dh->dccph_sport;
  135. ad->u.net->dport = dh->dccph_dport;
  136. break;
  137. }
  138. case IPPROTO_SCTP: {
  139. struct sctphdr _sctph, *sh;
  140. sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
  141. if (sh == NULL)
  142. break;
  143. ad->u.net->sport = sh->source;
  144. ad->u.net->dport = sh->dest;
  145. break;
  146. }
  147. default:
  148. ret = -EINVAL;
  149. }
  150. return ret;
  151. }
  152. #endif
  153. static inline void print_ipv6_addr(struct audit_buffer *ab,
  154. const struct in6_addr *addr, __be16 port,
  155. char *name1, char *name2)
  156. {
  157. if (!ipv6_addr_any(addr))
  158. audit_log_format(ab, " %s=%pI6c", name1, addr);
  159. if (port)
  160. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  161. }
  162. static inline void print_ipv4_addr(struct audit_buffer *ab, __be32 addr,
  163. __be16 port, char *name1, char *name2)
  164. {
  165. if (addr)
  166. audit_log_format(ab, " %s=%pI4", name1, &addr);
  167. if (port)
  168. audit_log_format(ab, " %s=%d", name2, ntohs(port));
  169. }
  170. /**
  171. * dump_common_audit_data - helper to dump common audit data
  172. * @ab : the audit buffer
  173. * @a : common audit data
  174. *
  175. */
  176. static void dump_common_audit_data(struct audit_buffer *ab,
  177. struct common_audit_data *a)
  178. {
  179. char comm[sizeof(current->comm)];
  180. /*
  181. * To keep stack sizes in check force programmers to notice if they
  182. * start making this union too large! See struct lsm_network_audit
  183. * as an example of how to deal with large data.
  184. */
  185. BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2);
  186. audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current));
  187. audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm)));
  188. switch (a->type) {
  189. case LSM_AUDIT_DATA_NONE:
  190. return;
  191. case LSM_AUDIT_DATA_IPC:
  192. audit_log_format(ab, " ipc_key=%d ", a->u.ipc_id);
  193. break;
  194. case LSM_AUDIT_DATA_CAP:
  195. audit_log_format(ab, " capability=%d ", a->u.cap);
  196. break;
  197. case LSM_AUDIT_DATA_PATH: {
  198. struct inode *inode;
  199. audit_log_d_path(ab, " path=", &a->u.path);
  200. inode = d_backing_inode(a->u.path.dentry);
  201. if (inode) {
  202. audit_log_format(ab, " dev=");
  203. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  204. audit_log_format(ab, " ino=%lu", inode->i_ino);
  205. }
  206. break;
  207. }
  208. case LSM_AUDIT_DATA_FILE: {
  209. struct inode *inode;
  210. audit_log_d_path(ab, " path=", &a->u.file->f_path);
  211. inode = file_inode(a->u.file);
  212. if (inode) {
  213. audit_log_format(ab, " dev=");
  214. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  215. audit_log_format(ab, " ino=%lu", inode->i_ino);
  216. }
  217. break;
  218. }
  219. case LSM_AUDIT_DATA_IOCTL_OP: {
  220. struct inode *inode;
  221. audit_log_d_path(ab, " path=", &a->u.op->path);
  222. inode = a->u.op->path.dentry->d_inode;
  223. if (inode) {
  224. audit_log_format(ab, " dev=");
  225. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  226. audit_log_format(ab, " ino=%lu", inode->i_ino);
  227. }
  228. audit_log_format(ab, " ioctlcmd=0x%hx", a->u.op->cmd);
  229. break;
  230. }
  231. case LSM_AUDIT_DATA_DENTRY: {
  232. struct inode *inode;
  233. audit_log_format(ab, " name=");
  234. spin_lock(&a->u.dentry->d_lock);
  235. audit_log_untrustedstring(ab, a->u.dentry->d_name.name);
  236. spin_unlock(&a->u.dentry->d_lock);
  237. inode = d_backing_inode(a->u.dentry);
  238. if (inode) {
  239. audit_log_format(ab, " dev=");
  240. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  241. audit_log_format(ab, " ino=%lu", inode->i_ino);
  242. }
  243. break;
  244. }
  245. case LSM_AUDIT_DATA_INODE: {
  246. struct dentry *dentry;
  247. struct inode *inode;
  248. rcu_read_lock();
  249. inode = a->u.inode;
  250. dentry = d_find_alias_rcu(inode);
  251. if (dentry) {
  252. audit_log_format(ab, " name=");
  253. spin_lock(&dentry->d_lock);
  254. audit_log_untrustedstring(ab, dentry->d_name.name);
  255. spin_unlock(&dentry->d_lock);
  256. }
  257. audit_log_format(ab, " dev=");
  258. audit_log_untrustedstring(ab, inode->i_sb->s_id);
  259. audit_log_format(ab, " ino=%lu", inode->i_ino);
  260. rcu_read_unlock();
  261. break;
  262. }
  263. case LSM_AUDIT_DATA_TASK: {
  264. struct task_struct *tsk = a->u.tsk;
  265. if (tsk) {
  266. pid_t pid = task_tgid_nr(tsk);
  267. if (pid) {
  268. char comm[sizeof(tsk->comm)];
  269. audit_log_format(ab, " opid=%d ocomm=", pid);
  270. audit_log_untrustedstring(ab,
  271. memcpy(comm, tsk->comm, sizeof(comm)));
  272. }
  273. }
  274. break;
  275. }
  276. case LSM_AUDIT_DATA_NET:
  277. if (a->u.net->sk) {
  278. const struct sock *sk = a->u.net->sk;
  279. const struct unix_sock *u;
  280. struct unix_address *addr;
  281. int len = 0;
  282. char *p = NULL;
  283. switch (sk->sk_family) {
  284. case AF_INET: {
  285. const struct inet_sock *inet = inet_sk(sk);
  286. print_ipv4_addr(ab, inet->inet_rcv_saddr,
  287. inet->inet_sport,
  288. "laddr", "lport");
  289. print_ipv4_addr(ab, inet->inet_daddr,
  290. inet->inet_dport,
  291. "faddr", "fport");
  292. break;
  293. }
  294. #if IS_ENABLED(CONFIG_IPV6)
  295. case AF_INET6: {
  296. const struct inet_sock *inet = inet_sk(sk);
  297. print_ipv6_addr(ab, &sk->sk_v6_rcv_saddr,
  298. inet->inet_sport,
  299. "laddr", "lport");
  300. print_ipv6_addr(ab, &sk->sk_v6_daddr,
  301. inet->inet_dport,
  302. "faddr", "fport");
  303. break;
  304. }
  305. #endif
  306. case AF_UNIX:
  307. u = unix_sk(sk);
  308. addr = smp_load_acquire(&u->addr);
  309. if (!addr)
  310. break;
  311. if (u->path.dentry) {
  312. audit_log_d_path(ab, " path=", &u->path);
  313. break;
  314. }
  315. len = addr->len-sizeof(short);
  316. p = &addr->name->sun_path[0];
  317. audit_log_format(ab, " path=");
  318. if (*p)
  319. audit_log_untrustedstring(ab, p);
  320. else
  321. audit_log_n_hex(ab, p, len);
  322. break;
  323. }
  324. }
  325. switch (a->u.net->family) {
  326. case AF_INET:
  327. print_ipv4_addr(ab, a->u.net->v4info.saddr,
  328. a->u.net->sport,
  329. "saddr", "src");
  330. print_ipv4_addr(ab, a->u.net->v4info.daddr,
  331. a->u.net->dport,
  332. "daddr", "dest");
  333. break;
  334. case AF_INET6:
  335. print_ipv6_addr(ab, &a->u.net->v6info.saddr,
  336. a->u.net->sport,
  337. "saddr", "src");
  338. print_ipv6_addr(ab, &a->u.net->v6info.daddr,
  339. a->u.net->dport,
  340. "daddr", "dest");
  341. break;
  342. }
  343. if (a->u.net->netif > 0) {
  344. struct net_device *dev;
  345. /* NOTE: we always use init's namespace */
  346. dev = dev_get_by_index(&init_net, a->u.net->netif);
  347. if (dev) {
  348. audit_log_format(ab, " netif=%s", dev->name);
  349. dev_put(dev);
  350. }
  351. }
  352. break;
  353. #ifdef CONFIG_KEYS
  354. case LSM_AUDIT_DATA_KEY:
  355. audit_log_format(ab, " key_serial=%u", a->u.key_struct.key);
  356. if (a->u.key_struct.key_desc) {
  357. audit_log_format(ab, " key_desc=");
  358. audit_log_untrustedstring(ab, a->u.key_struct.key_desc);
  359. }
  360. break;
  361. #endif
  362. case LSM_AUDIT_DATA_KMOD:
  363. audit_log_format(ab, " kmod=");
  364. audit_log_untrustedstring(ab, a->u.kmod_name);
  365. break;
  366. case LSM_AUDIT_DATA_IBPKEY: {
  367. struct in6_addr sbn_pfx;
  368. memset(&sbn_pfx.s6_addr, 0,
  369. sizeof(sbn_pfx.s6_addr));
  370. memcpy(&sbn_pfx.s6_addr, &a->u.ibpkey->subnet_prefix,
  371. sizeof(a->u.ibpkey->subnet_prefix));
  372. audit_log_format(ab, " pkey=0x%x subnet_prefix=%pI6c",
  373. a->u.ibpkey->pkey, &sbn_pfx);
  374. break;
  375. }
  376. case LSM_AUDIT_DATA_IBENDPORT:
  377. audit_log_format(ab, " device=%s port_num=%u",
  378. a->u.ibendport->dev_name,
  379. a->u.ibendport->port);
  380. break;
  381. case LSM_AUDIT_DATA_LOCKDOWN:
  382. audit_log_format(ab, " lockdown_reason=\"%s\"",
  383. lockdown_reasons[a->u.reason]);
  384. break;
  385. case LSM_AUDIT_DATA_ANONINODE:
  386. audit_log_format(ab, " anonclass=%s", a->u.anonclass);
  387. break;
  388. } /* switch (a->type) */
  389. }
  390. /**
  391. * common_lsm_audit - generic LSM auditing function
  392. * @a: auxiliary audit data
  393. * @pre_audit: lsm-specific pre-audit callback
  394. * @post_audit: lsm-specific post-audit callback
  395. *
  396. * setup the audit buffer for common security information
  397. * uses callback to print LSM specific information
  398. */
  399. void common_lsm_audit(struct common_audit_data *a,
  400. void (*pre_audit)(struct audit_buffer *, void *),
  401. void (*post_audit)(struct audit_buffer *, void *))
  402. {
  403. struct audit_buffer *ab;
  404. if (a == NULL)
  405. return;
  406. /* we use GFP_ATOMIC so we won't sleep */
  407. ab = audit_log_start(audit_context(), GFP_ATOMIC | __GFP_NOWARN,
  408. AUDIT_AVC);
  409. if (ab == NULL)
  410. return;
  411. if (pre_audit)
  412. pre_audit(ab, a);
  413. dump_common_audit_data(ab, a);
  414. if (post_audit)
  415. post_audit(ab, a);
  416. audit_log_end(ab);
  417. }