qtaguid.c revision 243123fb41d7d2981116bd5d11ba168b127d68e9
1/* 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
18#define LOG_TAG "qtaguid"
19
20#include <cutils/qtaguid.h>
21#include <cutils/log.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27#include <pthread.h>
28
29static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl";
30static const int CTRL_MAX_INPUT_LEN = 128;
31static const char *GLOBAL_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/passive";
32static const char *TAG_PACIFIER_PARAM = "/sys/module/xt_qtaguid/parameters/tag_tracking_passive";
33
34/*
35 * One per proccess.
36 * Once the device is open, this process will have its socket tags tracked.
37 * And on exit or untimely death, all socket tags will be removed.
38 * A process can only open /dev/xt_qtaguid once.
39 * It should not close it unless it is really done with all the socket tags.
40 * Failure to open it will be visible when socket tagging will be attempted.
41 */
42static int resTrackFd = -1;
43pthread_once_t resTrackInitDone = PTHREAD_ONCE_INIT;
44
45/* Only call once per process. */
46void qtaguid_resTrack(void) {
47    resTrackFd = TEMP_FAILURE_RETRY(open("/dev/xt_qtaguid", O_RDONLY));
48    if (resTrackFd >=0) {
49        TEMP_FAILURE_RETRY(fcntl(resTrackFd, F_SETFD, FD_CLOEXEC));
50    }
51}
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
61    LOGI("write_ctrl(%s)", cmd);
62
63    fd = TEMP_FAILURE_RETRY(open(CTRL_PROCPATH, O_WRONLY));
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        LOGI("Failed write_ctrl(%s) res=%d errno=%d", cmd, res, savedErrno);
76    }
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
85    param_fd = TEMP_FAILURE_RETRY(open(param_path, O_WRONLY));
86    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;
100    /* Doing java-land a favor, enforcing "long" */
101    uint64_t kTag = ((uint64_t)tag << 32) & ~(1LLU<<63);
102
103
104    pthread_once(&resTrackInitDone, qtaguid_resTrack);
105
106    snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid);
107
108    LOGI("Tagging socket %d with tag %llx{%u,0} for uid %d", sockfd, kTag, tag, uid);
109
110    res = write_ctrl(lineBuf);
111    if (res < 0) {
112        LOGI("Tagging socket %d with tag %llx(%d) for uid %d failed errno=%d",
113             sockfd, kTag, tag, uid, res);
114    }
115
116    return res;
117}
118
119int qtaguid_untagSocket(int sockfd) {
120    char lineBuf[CTRL_MAX_INPUT_LEN];
121    int res;
122
123    LOGI("Untagging socket %d", sockfd);
124
125    snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd);
126    res = write_ctrl(lineBuf);
127    if (res < 0) {
128        LOGI("Untagging socket %d failed errno=%d", sockfd, res);
129    }
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
138    LOGI("Setting counters to set %d for uid %d", counterSetNum, uid);
139
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
150    LOGI("Deleting tag data with tag %llx{%d,0} for uid %d", kTag, tag, uid);
151
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) {
157        LOGI("Deleteing tag data with tag %llx/%d for uid %d failed with cnt=%d errno=%d",
158             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) {
171        return -errno;
172    }
173    if (write_param(TAG_PACIFIER_PARAM, value) < 0) {
174        return -errno;
175    }
176    return 0;
177}
178