blob: 14a58ca746c92aff407ee6dde7fb7b9b8aa058e2 [file] [log] [blame]
Mark Salyzyn12717162014-04-29 15:49:14 -07001/*
Ashish Sharma8626cce2011-07-07 18:16:01 -07002** 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
JP Abgrall29656d32011-09-22 14:25:10 -070017// #define LOG_NDEBUG 0
18
Ashish Sharma8626cce2011-07-07 18:16:01 -070019#define LOG_TAG "qtaguid"
20
Ashish Sharma9b5c7742011-08-03 00:31:19 -070021#include <errno.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070022#include <fcntl.h>
Mark Salyzyn0425b642014-03-07 15:08:20 -080023#include <inttypes.h>
24#include <pthread.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070025#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
Mark Salyzyn0425b642014-03-07 15:08:20 -080028
29#include <cutils/qtaguid.h>
30#include <log/log.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070031
JP Abgrall243123f2011-09-09 15:02:08 -070032static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl";
33static const int CTRL_MAX_INPUT_LEN = 128;
34static const char *GLOBAL_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/passive";
35static const char *TAG_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/tag_tracking_passive";
Ashish Sharma8626cce2011-07-07 18:16:01 -070036
JP Abgrall243123f2011-09-09 15:02:08 -070037/*
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 */
45static int resTrackFd = -1;
46pthread_once_t resTrackInitDone = PTHREAD_ONCE_INIT;
47
48/* Only call once per process. */
49void qtaguid_resTrack(void) {
Elliott Hughesb4de99c2016-02-19 18:13:51 -080050 resTrackFd = TEMP_FAILURE_RETRY(open("/dev/xt_qtaguid", O_RDONLY | O_CLOEXEC));
JP Abgrall243123f2011-09-09 15:02:08 -070051}
52
53/*
54 * Returns:
55 * 0 on success.
56 * -errno on failure.
57 */
58static int write_ctrl(const char *cmd) {
59 int fd, res, savedErrno;
60
Steve Block69f4cd72011-10-20 11:54:09 +010061 ALOGV("write_ctrl(%s)", cmd);
JP Abgrall243123f2011-09-09 15:02:08 -070062
Elliott Hughesb4de99c2016-02-19 18:13:51 -080063 fd = TEMP_FAILURE_RETRY(open(CTRL_PROCPATH, O_WRONLY | O_CLOEXEC));
Ashish Sharma9b5c7742011-08-03 00:31:19 -070064 if (fd < 0) {
65 return -errno;
66 }
Ashish Sharma8626cce2011-07-07 18:16:01 -070067
JP Abgrall243123f2011-09-09 15:02:08 -070068 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) {
Steve Blockfe71a612012-01-04 19:19:03 +000075 ALOGI("Failed write_ctrl(%s) res=%d errno=%d", cmd, res, savedErrno);
JP Abgrall243123f2011-09-09 15:02:08 -070076 }
77 close(fd);
78 return -savedErrno;
79}
80
81static int write_param(const char *param_path, const char *value) {
82 int param_fd;
83 int res;
84
Elliott Hughesb4de99c2016-02-19 18:13:51 -080085 param_fd = TEMP_FAILURE_RETRY(open(param_path, O_WRONLY | O_CLOEXEC));
JP Abgrall243123f2011-09-09 15:02:08 -070086 if (param_fd < 0) {
87 return -errno;
88 }
89 res = TEMP_FAILURE_RETRY(write(param_fd, value, strlen(value)));
90 if (res < 0) {
91 return -errno;
92 }
93 close(param_fd);
94 return 0;
95}
96
97int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
98 char lineBuf[CTRL_MAX_INPUT_LEN];
99 int res;
Jeff Sharkeye34b9172012-05-02 16:01:31 -0700100 uint64_t kTag = ((uint64_t)tag << 32);
JP Abgrall243123f2011-09-09 15:02:08 -0700101
102 pthread_once(&resTrackInitDone, qtaguid_resTrack);
103
Mark Salyzyn0425b642014-03-07 15:08:20 -0800104 snprintf(lineBuf, sizeof(lineBuf), "t %d %" PRIu64 " %d", sockfd, kTag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700105
Mark Salyzyn0425b642014-03-07 15:08:20 -0800106 ALOGV("Tagging socket %d with tag %" PRIx64 "{%u,0} for uid %d", sockfd, kTag, tag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700107
108 res = write_ctrl(lineBuf);
109 if (res < 0) {
Mark Salyzyn0425b642014-03-07 15:08:20 -0800110 ALOGI("Tagging socket %d with tag %" PRIx64 "(%d) for uid %d failed errno=%d",
JP Abgrall243123f2011-09-09 15:02:08 -0700111 sockfd, kTag, tag, uid, res);
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700112 }
113
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700114 return res;
115}
116
JP Abgrall243123f2011-09-09 15:02:08 -0700117int qtaguid_untagSocket(int sockfd) {
118 char lineBuf[CTRL_MAX_INPUT_LEN];
119 int res;
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700120
Steve Block69f4cd72011-10-20 11:54:09 +0100121 ALOGV("Untagging socket %d", sockfd);
JP Abgrall243123f2011-09-09 15:02:08 -0700122
123 snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd);
124 res = write_ctrl(lineBuf);
125 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +0000126 ALOGI("Untagging socket %d failed errno=%d", sockfd, res);
JP Abgrall243123f2011-09-09 15:02:08 -0700127 }
128
129 return res;
130}
131
132int qtaguid_setCounterSet(int counterSetNum, uid_t uid) {
133 char lineBuf[CTRL_MAX_INPUT_LEN];
134 int res;
135
Steve Block69f4cd72011-10-20 11:54:09 +0100136 ALOGV("Setting counters to set %d for uid %d", counterSetNum, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700137
138 snprintf(lineBuf, sizeof(lineBuf), "s %d %d", counterSetNum, uid);
139 res = write_ctrl(lineBuf);
140 return res;
141}
142
143int qtaguid_deleteTagData(int tag, uid_t uid) {
144 char lineBuf[CTRL_MAX_INPUT_LEN];
Mark Salyzyn12717162014-04-29 15:49:14 -0700145 int cnt = 0, res = 0;
JP Abgrall243123f2011-09-09 15:02:08 -0700146 uint64_t kTag = (uint64_t)tag << 32;
147
Mark Salyzyn0425b642014-03-07 15:08:20 -0800148 ALOGV("Deleting tag data with tag %" PRIx64 "{%d,0} for uid %d", kTag, tag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700149
150 pthread_once(&resTrackInitDone, qtaguid_resTrack);
151
Mark Salyzyn0425b642014-03-07 15:08:20 -0800152 snprintf(lineBuf, sizeof(lineBuf), "d %" PRIu64 " %d", kTag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700153 res = write_ctrl(lineBuf);
154 if (res < 0) {
Mark Salyzyn0425b642014-03-07 15:08:20 -0800155 ALOGI("Deleting tag data with tag %" PRIx64 "/%d for uid %d failed with cnt=%d errno=%d",
JP Abgrall243123f2011-09-09 15:02:08 -0700156 kTag, tag, uid, cnt, errno);
157 }
158
159 return res;
160}
161
162int qtaguid_setPacifier(int on) {
JP Abgrall243123f2011-09-09 15:02:08 -0700163 const char *value;
164
165 value = on ? "Y" : "N";
166 if (write_param(GLOBAL_PACIFIER_PARAM, value) < 0) {
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700167 return -errno;
168 }
JP Abgrall243123f2011-09-09 15:02:08 -0700169 if (write_param(TAG_PACIFIER_PARAM, value) < 0) {
170 return -errno;
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700171 }
JP Abgrall243123f2011-09-09 15:02:08 -0700172 return 0;
Ashish Sharma8626cce2011-07-07 18:16:01 -0700173}