1 /* 2 ** Copyright 2011, The Android Open Source Project 3 ** 4 ** Licensed under the Apache License, Version 2.0 (the "License"); 5 ** you may not use this file except in compliance with the License. 6 ** You may obtain a copy of the License at 7 ** 8 ** http://www.apache.org/licenses/LICENSE-2.0 9 ** 10 ** Unless required by applicable law or agreed to in writing, software 11 ** distributed under the License is distributed on an "AS IS" BASIS, 12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 ** See the License for the specific language governing permissions and 14 ** limitations under the License. 15 */ 16 17 // #define LOG_NDEBUG 0 18 19 #define LOG_TAG "qtaguid" 20 21 #include <errno.h> 22 #include <fcntl.h> 23 #include <inttypes.h> 24 #include <pthread.h> 25 #include <stdio.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 #include <log/log.h> 30 #include <cutils/qtaguid.h> 31 32 static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl"; 33 static const int CTRL_MAX_INPUT_LEN = 128; 34 static const char *GLOBAL_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/passive"; 35 static const char *TAG_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/tag_tracking_passive"; 36 37 /* 38 * One per proccess. 39 * Once the device is open, this process will have its socket tags tracked. 40 * And on exit or untimely death, all socket tags will be removed. 41 * A process can only open /dev/xt_qtaguid once. 42 * It should not close it unless it is really done with all the socket tags. 43 * Failure to open it will be visible when socket tagging will be attempted. 44 */ 45 static int resTrackFd = -1; 46 pthread_once_t resTrackInitDone = PTHREAD_ONCE_INIT; 47 48 /* Only call once per process. */ 49 void qtaguid_resTrack(void) { 50 resTrackFd = TEMP_FAILURE_RETRY(open("/dev/xt_qtaguid", O_RDONLY | O_CLOEXEC)); 51 } 52 53 /* 54 * Returns: 55 * 0 on success. 56 * -errno on failure. 57 */ 58 static int write_ctrl(const char *cmd) { 59 int fd, res, savedErrno; 60 61 ALOGV("write_ctrl(%s)", cmd); 62 63 fd = TEMP_FAILURE_RETRY(open(CTRL_PROCPATH, O_WRONLY | O_CLOEXEC)); 64 if (fd < 0) { 65 return -errno; 66 } 67 68 res = TEMP_FAILURE_RETRY(write(fd, cmd, strlen(cmd))); 69 if (res < 0) { 70 savedErrno = errno; 71 } else { 72 savedErrno = 0; 73 } 74 if (res < 0) { 75 // ALOGV is enough because all the callers also log failures 76 ALOGV("Failed write_ctrl(%s) res=%d errno=%d", cmd, res, savedErrno); 77 } 78 close(fd); 79 return -savedErrno; 80 } 81 82 static int write_param(const char *param_path, const char *value) { 83 int param_fd; 84 int res; 85 86 param_fd = TEMP_FAILURE_RETRY(open(param_path, O_WRONLY | O_CLOEXEC)); 87 if (param_fd < 0) { 88 return -errno; 89 } 90 res = TEMP_FAILURE_RETRY(write(param_fd, value, strlen(value))); 91 if (res < 0) { 92 return -errno; 93 } 94 close(param_fd); 95 return 0; 96 } 97 98 int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) { 99 char lineBuf[CTRL_MAX_INPUT_LEN]; 100 int res; 101 uint64_t kTag = ((uint64_t)tag << 32); 102 103 pthread_once(&resTrackInitDone, qtaguid_resTrack); 104 105 snprintf(lineBuf, sizeof(lineBuf), "t %d %" PRIu64 " %d", sockfd, kTag, uid); 106 107 ALOGV("Tagging socket %d with tag %" PRIx64 "{%u,0} for uid %d", sockfd, kTag, tag, uid); 108 109 res = write_ctrl(lineBuf); 110 if (res < 0) { 111 ALOGI("Tagging socket %d with tag %" PRIx64 "(%d) for uid %d failed errno=%d", 112 sockfd, kTag, tag, uid, res); 113 } 114 115 return res; 116 } 117 118 int qtaguid_untagSocket(int sockfd) { 119 char lineBuf[CTRL_MAX_INPUT_LEN]; 120 int res; 121 122 ALOGV("Untagging socket %d", sockfd); 123 124 snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd); 125 res = write_ctrl(lineBuf); 126 if (res < 0) { 127 ALOGI("Untagging socket %d failed errno=%d", sockfd, res); 128 } 129 130 return res; 131 } 132 133 int qtaguid_setCounterSet(int counterSetNum, uid_t uid) { 134 char lineBuf[CTRL_MAX_INPUT_LEN]; 135 int res; 136 137 ALOGV("Setting counters to set %d for uid %d", counterSetNum, uid); 138 139 snprintf(lineBuf, sizeof(lineBuf), "s %d %d", counterSetNum, uid); 140 res = write_ctrl(lineBuf); 141 return res; 142 } 143 144 int qtaguid_deleteTagData(int tag, uid_t uid) { 145 char lineBuf[CTRL_MAX_INPUT_LEN]; 146 int cnt = 0, res = 0; 147 uint64_t kTag = (uint64_t)tag << 32; 148 149 ALOGV("Deleting tag data with tag %" PRIx64 "{%d,0} for uid %d", kTag, tag, uid); 150 151 pthread_once(&resTrackInitDone, qtaguid_resTrack); 152 153 snprintf(lineBuf, sizeof(lineBuf), "d %" PRIu64 " %d", kTag, uid); 154 res = write_ctrl(lineBuf); 155 if (res < 0) { 156 ALOGI("Deleting tag data with tag %" PRIx64 "/%d for uid %d failed with cnt=%d errno=%d", 157 kTag, tag, uid, cnt, errno); 158 } 159 160 return res; 161 } 162 163 int qtaguid_setPacifier(int on) { 164 const char *value; 165 166 value = on ? "Y" : "N"; 167 if (write_param(GLOBAL_PACIFIER_PARAM, value) < 0) { 168 return -errno; 169 } 170 if (write_param(TAG_PACIFIER_PARAM, value) < 0) { 171 return -errno; 172 } 173 return 0; 174 } 175