blob: 5bb81761ed880224b81ada3a74d602e6487ab39d [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
22#include <cutils/qtaguid.h>
23#include <cutils/log.h>
Ashish Sharma9b5c7742011-08-03 00:31:19 -070024#include <errno.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070025#include <fcntl.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
JP Abgrall243123f2011-09-09 15:02:08 -070029#include <pthread.h>
Ashish Sharma8626cce2011-07-07 18:16:01 -070030
JP Abgrall243123f2011-09-09 15:02:08 -070031static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl";
32static const int CTRL_MAX_INPUT_LEN = 128;
33static const char *GLOBAL_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/passive";
34static const char *TAG_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/tag_tracking_passive";
Ashish Sharma8626cce2011-07-07 18:16:01 -070035
JP Abgrall243123f2011-09-09 15:02:08 -070036/*
37 * One per proccess.
38 * Once the device is open, this process will have its socket tags tracked.
39 * And on exit or untimely death, all socket tags will be removed.
40 * A process can only open /dev/xt_qtaguid once.
41 * It should not close it unless it is really done with all the socket tags.
42 * Failure to open it will be visible when socket tagging will be attempted.
43 */
44static int resTrackFd = -1;
45pthread_once_t resTrackInitDone = PTHREAD_ONCE_INIT;
46
47/* Only call once per process. */
48void qtaguid_resTrack(void) {
49 resTrackFd = TEMP_FAILURE_RETRY(open("/dev/xt_qtaguid", O_RDONLY));
50 if (resTrackFd >=0) {
51 TEMP_FAILURE_RETRY(fcntl(resTrackFd, F_SETFD, FD_CLOEXEC));
52 }
53}
54
55/*
56 * Returns:
57 * 0 on success.
58 * -errno on failure.
59 */
60static int write_ctrl(const char *cmd) {
61 int fd, res, savedErrno;
62
Steve Block69f4cd72011-10-20 11:54:09 +010063 ALOGV("write_ctrl(%s)", cmd);
JP Abgrall243123f2011-09-09 15:02:08 -070064
65 fd = TEMP_FAILURE_RETRY(open(CTRL_PROCPATH, O_WRONLY));
Ashish Sharma9b5c7742011-08-03 00:31:19 -070066 if (fd < 0) {
67 return -errno;
68 }
Ashish Sharma8626cce2011-07-07 18:16:01 -070069
JP Abgrall243123f2011-09-09 15:02:08 -070070 res = TEMP_FAILURE_RETRY(write(fd, cmd, strlen(cmd)));
71 if (res < 0) {
72 savedErrno = errno;
73 } else {
74 savedErrno = 0;
75 }
76 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +000077 ALOGI("Failed write_ctrl(%s) res=%d errno=%d", cmd, res, savedErrno);
JP Abgrall243123f2011-09-09 15:02:08 -070078 }
79 close(fd);
80 return -savedErrno;
81}
82
83static int write_param(const char *param_path, const char *value) {
84 int param_fd;
85 int res;
86
87 param_fd = TEMP_FAILURE_RETRY(open(param_path, O_WRONLY));
88 if (param_fd < 0) {
89 return -errno;
90 }
91 res = TEMP_FAILURE_RETRY(write(param_fd, value, strlen(value)));
92 if (res < 0) {
93 return -errno;
94 }
95 close(param_fd);
96 return 0;
97}
98
99int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
100 char lineBuf[CTRL_MAX_INPUT_LEN];
101 int res;
Jeff Sharkeye34b9172012-05-02 16:01:31 -0700102 uint64_t kTag = ((uint64_t)tag << 32);
JP Abgrall243123f2011-09-09 15:02:08 -0700103
104 pthread_once(&resTrackInitDone, qtaguid_resTrack);
105
106 snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid);
107
Steve Block69f4cd72011-10-20 11:54:09 +0100108 ALOGV("Tagging socket %d with tag %llx{%u,0} for uid %d", sockfd, kTag, tag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700109
110 res = write_ctrl(lineBuf);
111 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +0000112 ALOGI("Tagging socket %d with tag %llx(%d) for uid %d failed errno=%d",
JP Abgrall243123f2011-09-09 15:02:08 -0700113 sockfd, kTag, tag, uid, res);
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700114 }
115
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700116 return res;
117}
118
JP Abgrall243123f2011-09-09 15:02:08 -0700119int qtaguid_untagSocket(int sockfd) {
120 char lineBuf[CTRL_MAX_INPUT_LEN];
121 int res;
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700122
Steve Block69f4cd72011-10-20 11:54:09 +0100123 ALOGV("Untagging socket %d", sockfd);
JP Abgrall243123f2011-09-09 15:02:08 -0700124
125 snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd);
126 res = write_ctrl(lineBuf);
127 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +0000128 ALOGI("Untagging socket %d failed errno=%d", sockfd, res);
JP Abgrall243123f2011-09-09 15:02:08 -0700129 }
130
131 return res;
132}
133
134int qtaguid_setCounterSet(int counterSetNum, uid_t uid) {
135 char lineBuf[CTRL_MAX_INPUT_LEN];
136 int res;
137
Steve Block69f4cd72011-10-20 11:54:09 +0100138 ALOGV("Setting counters to set %d for uid %d", counterSetNum, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700139
140 snprintf(lineBuf, sizeof(lineBuf), "s %d %d", counterSetNum, uid);
141 res = write_ctrl(lineBuf);
142 return res;
143}
144
145int qtaguid_deleteTagData(int tag, uid_t uid) {
146 char lineBuf[CTRL_MAX_INPUT_LEN];
147 int fd, cnt = 0, res = 0;
148 uint64_t kTag = (uint64_t)tag << 32;
149
Steve Block69f4cd72011-10-20 11:54:09 +0100150 ALOGV("Deleting tag data with tag %llx{%d,0} for uid %d", kTag, tag, uid);
JP Abgrall243123f2011-09-09 15:02:08 -0700151
152 pthread_once(&resTrackInitDone, qtaguid_resTrack);
153
154 snprintf(lineBuf, sizeof(lineBuf), "d %llu %d", kTag, uid);
155 res = write_ctrl(lineBuf);
156 if (res < 0) {
Steve Blockfe71a612012-01-04 19:19:03 +0000157 ALOGI("Deleteing tag data with tag %llx/%d for uid %d failed with cnt=%d errno=%d",
JP Abgrall243123f2011-09-09 15:02:08 -0700158 kTag, tag, uid, cnt, errno);
159 }
160
161 return res;
162}
163
164int qtaguid_setPacifier(int on) {
165 int param_fd;
166 int res;
167 const char *value;
168
169 value = on ? "Y" : "N";
170 if (write_param(GLOBAL_PACIFIER_PARAM, value) < 0) {
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700171 return -errno;
172 }
JP Abgrall243123f2011-09-09 15:02:08 -0700173 if (write_param(TAG_PACIFIER_PARAM, value) < 0) {
174 return -errno;
Ashish Sharma9b5c7742011-08-03 00:31:19 -0700175 }
JP Abgrall243123f2011-09-09 15:02:08 -0700176 return 0;
Ashish Sharma8626cce2011-07-07 18:16:01 -0700177}