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> |
| 18 | |
| 19 | #define MAX_BUF_SIZE 188 |
| 20 | |
| 21 | #define SEEMP_LOGK_API_SIZE sizeof(int) |
| 22 | |
| 23 | /* Write: api_id + skip encoding byte + params */ |
| 24 | #define SEEMP_LOGK_RECORD(api_id, format, ...) do { \ |
| 25 | *((int *)(buf - SEEMP_LOGK_API_SIZE)) = api_id; \ |
| 26 | snprintf(buf + 1, MAX_BUF_SIZE - 1, format, ##__VA_ARGS__); \ |
| 27 | } while (0) |
| 28 | |
| 29 | extern void *(*seemp_logk_kernel_begin)(char **buf); |
| 30 | extern void (*seemp_logk_kernel_end)(void *blck); |
| 31 | |
| 32 | static inline void *seemp_setup_buf(char **buf) |
| 33 | { |
| 34 | void *blck; |
| 35 | |
| 36 | if (seemp_logk_kernel_begin && seemp_logk_kernel_end) { |
| 37 | blck = seemp_logk_kernel_begin(buf); |
| 38 | if (!*buf) { |
| 39 | seemp_logk_kernel_end(blck); |
| 40 | return NULL; |
| 41 | } |
| 42 | } else { |
| 43 | return NULL; |
| 44 | } |
| 45 | return blck; |
| 46 | } |
| 47 | /* |
| 48 | * NOTE: only sendto is going to be instrumented |
| 49 | * since send sys call internally calls sendto |
| 50 | * with 2 extra parameters |
| 51 | */ |
| 52 | static inline void seemp_logk_sendto(int fd, void __user *buff, size_t len, |
| 53 | unsigned int flags, struct sockaddr __user *addr, int addr_len) |
| 54 | { |
| 55 | char *buf = NULL; |
| 56 | void *blck = NULL; |
| 57 | |
| 58 | /*sets up buf and blck correctly*/ |
| 59 | blck = seemp_setup_buf(&buf); |
| 60 | if (!blck) |
| 61 | return; |
| 62 | |
| 63 | /*fill the buf*/ |
| 64 | SEEMP_LOGK_RECORD(SEEMP_API_kernel__sendto, "len=%u,fd=%d", |
| 65 | (unsigned int)len, fd); |
| 66 | |
| 67 | seemp_logk_kernel_end(blck); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * NOTE: only recvfrom is going to be instrumented |
| 72 | * since recv sys call internally calls recvfrom |
| 73 | * with 2 extra parameters |
| 74 | */ |
| 75 | static inline void seemp_logk_recvfrom(int fd, void __user *ubuf, |
| 76 | size_t size, unsigned int flags, struct sockaddr __user *addr, |
| 77 | int __user *addr_len) |
| 78 | { |
| 79 | char *buf = NULL; |
| 80 | void *blck = NULL; |
| 81 | |
| 82 | /*sets up buf and blck correctly*/ |
| 83 | blck = seemp_setup_buf(&buf); |
| 84 | if (!blck) |
| 85 | return; |
| 86 | |
| 87 | /*fill the buf*/ |
| 88 | SEEMP_LOGK_RECORD(SEEMP_API_kernel__recvfrom, "size=%u,fd=%d", |
| 89 | (unsigned int)size, fd); |
| 90 | |
| 91 | seemp_logk_kernel_end(blck); |
| 92 | } |
| 93 | |
| 94 | static inline void seemp_logk_oom_adjust_write(pid_t pid, |
| 95 | kuid_t uid, int oom_adj) |
| 96 | { |
| 97 | char *buf = NULL; |
| 98 | void *blck = NULL; |
| 99 | |
| 100 | /*sets up buf and blck correctly*/ |
| 101 | blck = seemp_setup_buf(&buf); |
| 102 | if (!blck) |
| 103 | return; |
| 104 | |
| 105 | /*fill the buf*/ |
| 106 | SEEMP_LOGK_RECORD(SEEMP_API_kernel__oom_adjust_write, |
| 107 | "app_uid=%d,app_pid=%d,oom_adj=%d", |
| 108 | uid.val, pid, oom_adj); |
| 109 | |
| 110 | seemp_logk_kernel_end(blck); |
| 111 | } |
| 112 | |
| 113 | static inline void seemp_logk_oom_score_adj_write(pid_t pid, kuid_t uid, |
| 114 | int oom_adj_score) |
| 115 | { |
| 116 | char *buf = NULL; |
| 117 | void *blck = NULL; |
| 118 | |
| 119 | /*sets up buf and blck correctly*/ |
| 120 | blck = seemp_setup_buf(&buf); |
| 121 | if (!blck) |
| 122 | return; |
| 123 | |
| 124 | /*fill the buf*/ |
| 125 | snprintf(buf, MAX_BUF_SIZE, |
| 126 | "-1|kernel|oom_score_adj_write|app_uid=%d,app_pid=%d,oom_adj=%d|--end", |
| 127 | uid.val, pid, oom_adj_score); |
| 128 | |
| 129 | seemp_logk_kernel_end(blck); |
| 130 | } |
| 131 | |
| 132 | #else |
| 133 | static inline void seemp_logk_sendto(int fd, void __user *buff, |
| 134 | size_t len, unsigned int flags, struct sockaddr __user *addr, |
| 135 | int addr_len) |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | static inline void seemp_logk_recvfrom |
| 140 | (int fd, void __user *ubuf, size_t size, |
| 141 | unsigned int flags, struct sockaddr __user *addr, |
| 142 | int __user *addr_len) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | static inline void seemp_logk_oom_adjust_write |
| 147 | (pid_t pid, kuid_t uid, int oom_adj) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | static inline void seemp_logk_oom_score_adj_write |
| 152 | (pid_t pid, kuid_t uid, int oom_adj_score) |
| 153 | { |
| 154 | } |
| 155 | #endif |
| 156 | #endif |