Yida Wang | 0bf43bd | 2017-03-22 18:16:31 -0400 | [diff] [blame] | 1 | /* |
| 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 Wang | 943c3b4 | 2017-06-23 17:10:13 -0400 | [diff] [blame] | 18 | #include <linux/seemp_api.h> |
| 19 | #include <linux/socket.h> |
Yida Wang | 0bf43bd | 2017-03-22 18:16:31 -0400 | [diff] [blame] | 20 | |
| 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 | |
| 31 | extern void *(*seemp_logk_kernel_begin)(char **buf); |
| 32 | extern void (*seemp_logk_kernel_end)(void *blck); |
| 33 | |
| 34 | static 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 | */ |
| 54 | static 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 Wang | 943c3b4 | 2017-06-23 17:10:13 -0400 | [diff] [blame] | 71 | |
Yida Wang | 117c2c0 | 2017-08-09 17:19:57 -0400 | [diff] [blame] | 72 | static inline void seemp_logk_rtic(__u8 type, pid_t pid, __u8 asset_id[0x20], |
Yida Wang | 943c3b4 | 2017-06-23 17:10:13 -0400 | [diff] [blame] | 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, |
Yida Wang | 117c2c0 | 2017-08-09 17:19:57 -0400 | [diff] [blame] | 83 | "app_pid=%d,rtic_type=%u,asset_id=%s,asset_category=%u,response=%u", |
| 84 | pid, type, asset_id, asset_category, response); |
Yida Wang | 943c3b4 | 2017-06-23 17:10:13 -0400 | [diff] [blame] | 85 | |
| 86 | seemp_logk_kernel_end(blck); |
| 87 | } |
Yida Wang | 0bf43bd | 2017-03-22 18:16:31 -0400 | [diff] [blame] | 88 | #else |
| 89 | static 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 Wang | 943c3b4 | 2017-06-23 17:10:13 -0400 | [diff] [blame] | 94 | |
| 95 | static inline void seemp_logk_rtic(__u8 type, __u64 actor, __u8 asset_id[0x20], |
| 96 | __u8 asset_category, __u8 response) |
| 97 | { |
| 98 | } |
Yida Wang | 0bf43bd | 2017-03-22 18:16:31 -0400 | [diff] [blame] | 99 | #endif |
| 100 | #endif |