blob: 899a7b44f04e845bdbc1374173ac376ec7371c5b [file] [log] [blame]
Ashish Sharma8626cce2011-07-07 18:16:01 -07001/* libcutils/qtaguid.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
JP Abgrall29656d32011-09-22 14:25:10 -070018// #define LOG_NDEBUG 0
19
Ashish Sharma8626cce2011-07-07 18:16:01 -070020#define LOG_TAG "qtaguid"
21
Ashish Sharma9b5c7742011-08-03 00:31:19 -070022#include <errno.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070023#include <fcntl.h>
Mark Salyzyn0425b642014-03-07 15:08:20 -080024#include <inttypes.h>
25#include <pthread.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070026#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
Mark Salyzyn0425b642014-03-07 15:08:20 -080029
30#include <cutils/qtaguid.h>
31#include <log/log.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070032
JP Abgrall243123f2011-09-09 15:02:08 -070033static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl";
34static const int CTRL_MAX_INPUT_LEN = 128;
35static const char *GLOBAL_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/passive";
36static const char *TAG_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/tag_tracking_passive";
Ashish Sharma8626cce2011-07-07 18:16:01 -070037
JP Abgrall243123f2011-09-09 15:02:08 -070038/*
39 * One per proccess.
40 * Once the device is open, this process will have its socket tags tracked.
41 * And on exit or untimely death, all socket tags will be removed.
42 * A process can only open /dev/xt_qtaguid once.
43 * It should not close it unless it is really done with all the socket tags.
44 * Failure to open it will be visible when socket tagging will be attempted.
45 */
46static int resTrackFd = -1;
47pthread_once_t resTrackInitDone = PTHREAD_ONCE_INIT;
48
49/* Only call once per process. */
50void qtaguid_resTrack(void) {
51 resTrackFd = TEMP_FAILURE_RETRY(open("/dev/xt_qtaguid", O_RDONLY));
52 if (resTrackFd >=0) {
53 TEMP_FAILURE_RETRY(fcntl(resTrackFd, F_SETFD, FD_CLOEXEC));
54 }
55}
56
57/*
58 * Returns:
59 * 0 on success.
60 * -errno on failure.
61 */
62static int write_ctrl(const char *cmd) {
63 int fd, res, savedErrno;
64
Steve Block69f4cd72011-10-20 11:54:09 +010065 ALOGV("write_ctrl(%s)", cmd);
JP Abgrall243123f2011-09-09 15:02:08 -070066
67 fd = TEMP_FAILURE_RETRY(open(CTRL_PROCPATH, O_WRONLY));
Ashish Sharma9b5c7742011-08-03 00:31:19 -070068 if (fd < 0) {
69 return -errno;
70 }
Ashish Sharma8626cce2011-07-07 18:16:01 -070071
JP Abgrall243123f2011-09-09 15:02:08 -070072 res = TEMP_FAILURE_RETRY(write(fd, cmd, strlen(cmd)));
73 if (res < 0) {
74 savedErrno = errno;
75 } else {
76 savedErrno = 0;
77 }
78 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +000079 ALOGI("Failed write_ctrl(%s) res=%d errno=%d", cmd, res, savedErrno);
JP Abgrall243123f2011-09-09 15:02:08 -070080 }
81 close(fd);
82 return -savedErrno;
83}
84
85static int write_param(const char *param_path, const char *value) {
86 int param_fd;
87 int res;
88
89 param_fd = TEMP_FAILURE_RETRY(open(param_path, O_WRONLY));
90 if (param_fd < 0) {
91 return -errno;
92 }
93 res = TEMP_FAILURE_RETRY(write(param_fd, value, strlen(value)));
94 if (res < 0) {
95 return -errno;
96 }
97 close(param_fd);
98 return 0;
99}
100
101int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
102 char lineBuf[CTRL_MAX_INPUT_LEN];
103 int res;
Jeff Sharkeye34b9172012-05-02 16:01:31 -0700104 uint64_t kTag = ((uint64_t)tag << 32);
JP Abgrall243123f2011-09-09 15:02:08 -0700105
106 pthread_once(&resTrackInitDone, qtaguid_resTrack);
107
Mark Salyzyn0425b642014-03-07 15:08:20 -0800108 snprintf(lineBuf, sizeof(lineBuf), "t %d %" PRIu64 " %d", sockfd, kTag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700109
Mark Salyzyn0425b642014-03-07 15:08:20 -0800110 ALOGV("Tagging socket %d with tag %" PRIx64 "{%u,0} for uid %d", sockfd, kTag, tag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700111
112 res = write_ctrl(lineBuf);
113 if (res < 0) {
Mark Salyzyn0425b642014-03-07 15:08:20 -0800114 ALOGI("Tagging socket %d with tag %" PRIx64 "(%d) for uid %d failed errno=%d",
JP Abgrall243123f2011-09-09 15:02:08 -0700115 sockfd, kTag, tag, uid, res);
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700116 }
117
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700118 return res;
119}
120
JP Abgrall243123f2011-09-09 15:02:08 -0700121int qtaguid_untagSocket(int sockfd) {
122 char lineBuf[CTRL_MAX_INPUT_LEN];
123 int res;
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700124
Steve Block69f4cd72011-10-20 11:54:09 +0100125 ALOGV("Untagging socket %d", sockfd);
JP Abgrall243123f2011-09-09 15:02:08 -0700126
127 snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd);
128 res = write_ctrl(lineBuf);
129 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +0000130 ALOGI("Untagging socket %d failed errno=%d", sockfd, res);
JP Abgrall243123f2011-09-09 15:02:08 -0700131 }
132
133 return res;
134}
135
136int qtaguid_setCounterSet(int counterSetNum, uid_t uid) {
137 char lineBuf[CTRL_MAX_INPUT_LEN];
138 int res;
139
Steve Block69f4cd72011-10-20 11:54:09 +0100140 ALOGV("Setting counters to set %d for uid %d", counterSetNum, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700141
142 snprintf(lineBuf, sizeof(lineBuf), "s %d %d", counterSetNum, uid);
143 res = write_ctrl(lineBuf);
144 return res;
145}
146
147int qtaguid_deleteTagData(int tag, uid_t uid) {
148 char lineBuf[CTRL_MAX_INPUT_LEN];
149 int fd, cnt = 0, res = 0;
150 uint64_t kTag = (uint64_t)tag << 32;
151
Mark Salyzyn0425b642014-03-07 15:08:20 -0800152 ALOGV("Deleting tag data with tag %" PRIx64 "{%d,0} for uid %d", kTag, tag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700153
154 pthread_once(&resTrackInitDone, qtaguid_resTrack);
155
Mark Salyzyn0425b642014-03-07 15:08:20 -0800156 snprintf(lineBuf, sizeof(lineBuf), "d %" PRIu64 " %d", kTag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700157 res = write_ctrl(lineBuf);
158 if (res < 0) {
Mark Salyzyn0425b642014-03-07 15:08:20 -0800159 ALOGI("Deleting tag data with tag %" PRIx64 "/%d for uid %d failed with cnt=%d errno=%d",
JP Abgrall243123f2011-09-09 15:02:08 -0700160 kTag, tag, uid, cnt, errno);
161 }
162
163 return res;
164}
165
166int qtaguid_setPacifier(int on) {
167 int param_fd;
168 int res;
169 const char *value;
170
171 value = on ? "Y" : "N";
172 if (write_param(GLOBAL_PACIFIER_PARAM, value) < 0) {
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700173 return -errno;
174 }
JP Abgrall243123f2011-09-09 15:02:08 -0700175 if (write_param(TAG_PACIFIER_PARAM, value) < 0) {
176 return -errno;
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700177 }
JP Abgrall243123f2011-09-09 15:02:08 -0700178 return 0;
Ashish Sharma8626cce2011-07-07 18:16:01 -0700179}