blob: ff09bd2ab8fe6a398f49f6a665980ebf9f2f2cb4 [file] [log] [blame]
Yida Wang0bf43bd2017-03-22 18:16:31 -04001/*
2 * Copyright (c) 2015, 2017, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13#ifndef __SEEMP_LOGK_STUB__
14#define __SEEMP_LOGK_STUB__
15
16#ifdef CONFIG_SEEMP_CORE
17#include <linux/kernel.h>
Yida Wang943c3b42017-06-23 17:10:13 -040018#include <linux/seemp_api.h>
19#include <linux/socket.h>
Yida Wang0bf43bd2017-03-22 18:16:31 -040020
21#define MAX_BUF_SIZE 188
22
23#define SEEMP_LOGK_API_SIZE sizeof(int)
24
25/* Write: api_id + skip encoding byte + params */
26#define SEEMP_LOGK_RECORD(api_id, format, ...) do { \
27 *((int *)(buf - SEEMP_LOGK_API_SIZE)) = api_id; \
28 snprintf(buf + 1, MAX_BUF_SIZE - 1, format, ##__VA_ARGS__); \
29} while (0)
30
31extern void *(*seemp_logk_kernel_begin)(char **buf);
32extern void (*seemp_logk_kernel_end)(void *blck);
33
34static inline void *seemp_setup_buf(char **buf)
35{
36 void *blck;
37
38 if (seemp_logk_kernel_begin && seemp_logk_kernel_end) {
39 blck = seemp_logk_kernel_begin(buf);
40 if (!*buf) {
41 seemp_logk_kernel_end(blck);
42 return NULL;
43 }
44 } else {
45 return NULL;
46 }
47 return blck;
48}
49/*
50 * NOTE: only sendto is going to be instrumented
51 * since send sys call internally calls sendto
52 * with 2 extra parameters
53 */
54static inline void seemp_logk_sendto(int fd, void __user *buff, size_t len,
55 unsigned int flags, struct sockaddr __user *addr, int addr_len)
56{
57 char *buf = NULL;
58 void *blck = NULL;
59
60 /*sets up buf and blck correctly*/
61 blck = seemp_setup_buf(&buf);
62 if (!blck)
63 return;
64
65 /*fill the buf*/
66 SEEMP_LOGK_RECORD(SEEMP_API_kernel__sendto, "len=%u,fd=%d",
67 (unsigned int)len, fd);
68
69 seemp_logk_kernel_end(blck);
70}
Yida Wang943c3b42017-06-23 17:10:13 -040071
72static inline void seemp_logk_rtic(__u8 type, __u64 actor, __u8 asset_id[0x20],
73 __u8 asset_category, __u8 response)
74{
75 char *buf = NULL;
76 void *blck = NULL;
77
78 blck = seemp_setup_buf(&buf);
79 if (!blck)
80 return;
81
82 SEEMP_LOGK_RECORD(SEEMP_API_kernel__rtic,
83 "app_pid=%llu,rtic_type=%u,asset_id=%s,asset_category=%u,response=%u",
84 actor, type, asset_id, asset_category, response);
85
86 seemp_logk_kernel_end(blck);
87}
Yida Wang0bf43bd2017-03-22 18:16:31 -040088#else
89static inline void seemp_logk_sendto(int fd, void __user *buff,
90 size_t len, unsigned int flags, struct sockaddr __user *addr,
91 int addr_len)
92{
93}
Yida Wang943c3b42017-06-23 17:10:13 -040094
95static inline void seemp_logk_rtic(__u8 type, __u64 actor, __u8 asset_id[0x20],
96 __u8 asset_category, __u8 response)
97{
98}
Yida Wang0bf43bd2017-03-22 18:16:31 -040099#endif
100#endif