Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 1 | /* |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 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 | |
JP Abgrall | 29656d3 | 2011-09-22 14:25:10 -0700 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 19 | #define LOG_TAG "qtaguid" |
| 20 | |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | #include <pthread.h> |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 28 | |
Chenbo Feng | 5b11831 | 2017-10-25 11:23:50 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 30 | #include <qtaguid/qtaguid.h> |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 31 | |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 32 | static const char* CTRL_PROCPATH = "/proc/net/xt_qtaguid/ctrl"; |
| 33 | static const int CTRL_MAX_INPUT_LEN = 128; |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 34 | |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 35 | /* |
| 36 | * One per proccess. |
| 37 | * Once the device is open, this process will have its socket tags tracked. |
| 38 | * And on exit or untimely death, all socket tags will be removed. |
| 39 | * A process can only open /dev/xt_qtaguid once. |
| 40 | * It should not close it unless it is really done with all the socket tags. |
| 41 | * Failure to open it will be visible when socket tagging will be attempted. |
| 42 | */ |
| 43 | static int resTrackFd = -1; |
| 44 | pthread_once_t resTrackInitDone = PTHREAD_ONCE_INIT; |
| 45 | |
| 46 | /* Only call once per process. */ |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 47 | void legacy_resTrack(void) { |
Elliott Hughes | b4de99c | 2016-02-19 18:13:51 -0800 | [diff] [blame] | 48 | resTrackFd = TEMP_FAILURE_RETRY(open("/dev/xt_qtaguid", O_RDONLY | O_CLOEXEC)); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Returns: |
| 53 | * 0 on success. |
| 54 | * -errno on failure. |
| 55 | */ |
Chenbo Feng | 5b11831 | 2017-10-25 11:23:50 -0700 | [diff] [blame] | 56 | static int write_ctrl(const char* cmd) { |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 57 | int fd, res, savedErrno; |
| 58 | |
Steve Block | 69f4cd7 | 2011-10-20 11:54:09 +0100 | [diff] [blame] | 59 | ALOGV("write_ctrl(%s)", cmd); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | b4de99c | 2016-02-19 18:13:51 -0800 | [diff] [blame] | 61 | fd = TEMP_FAILURE_RETRY(open(CTRL_PROCPATH, O_WRONLY | O_CLOEXEC)); |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame] | 62 | if (fd < 0) { |
| 63 | return -errno; |
| 64 | } |
Ashish Sharma | 8626cce | 2011-07-07 18:16:01 -0700 | [diff] [blame] | 65 | |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 66 | res = TEMP_FAILURE_RETRY(write(fd, cmd, strlen(cmd))); |
| 67 | if (res < 0) { |
| 68 | savedErrno = errno; |
| 69 | } else { |
| 70 | savedErrno = 0; |
| 71 | } |
| 72 | if (res < 0) { |
Joe Onorato | b03b6ac | 2016-03-18 13:40:18 -0700 | [diff] [blame] | 73 | // ALOGV is enough because all the callers also log failures |
| 74 | ALOGV("Failed write_ctrl(%s) res=%d errno=%d", cmd, res, savedErrno); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 75 | } |
| 76 | close(fd); |
| 77 | return -savedErrno; |
| 78 | } |
| 79 | |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 80 | int legacy_tagSocket(int sockfd, int tag, uid_t uid) { |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 81 | char lineBuf[CTRL_MAX_INPUT_LEN]; |
| 82 | int res; |
Jeff Sharkey | e34b917 | 2012-05-02 16:01:31 -0700 | [diff] [blame] | 83 | uint64_t kTag = ((uint64_t)tag << 32); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 84 | |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 85 | pthread_once(&resTrackInitDone, legacy_resTrack); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 86 | |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 87 | snprintf(lineBuf, sizeof(lineBuf), "t %d %" PRIu64 " %d", sockfd, kTag, uid); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 88 | |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 89 | ALOGV("Tagging socket %d with tag %" PRIx64 "{%u,0} for uid %d", sockfd, kTag, tag, uid); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 90 | |
| 91 | res = write_ctrl(lineBuf); |
| 92 | if (res < 0) { |
Chenbo Feng | 5b11831 | 2017-10-25 11:23:50 -0700 | [diff] [blame] | 93 | ALOGI("Tagging socket %d with tag %" PRIx64 "(%d) for uid %d failed errno=%d", sockfd, kTag, |
| 94 | tag, uid, res); |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame] | 97 | return res; |
| 98 | } |
| 99 | |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 100 | int legacy_untagSocket(int sockfd) { |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 101 | char lineBuf[CTRL_MAX_INPUT_LEN]; |
| 102 | int res; |
Ashish Sharma | 9b5c774 | 2011-08-03 00:31:19 -0700 | [diff] [blame] | 103 | |
Steve Block | 69f4cd7 | 2011-10-20 11:54:09 +0100 | [diff] [blame] | 104 | ALOGV("Untagging socket %d", sockfd); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 105 | |
| 106 | snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd); |
| 107 | res = write_ctrl(lineBuf); |
| 108 | if (res < 0) { |
Steve Block | fe71a61 | 2012-01-04 19:19:03 +0000 | [diff] [blame] | 109 | ALOGI("Untagging socket %d failed errno=%d", sockfd, res); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return res; |
| 113 | } |
| 114 | |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 115 | int legacy_setCounterSet(int counterSetNum, uid_t uid) { |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 116 | char lineBuf[CTRL_MAX_INPUT_LEN]; |
| 117 | int res; |
| 118 | |
Steve Block | 69f4cd7 | 2011-10-20 11:54:09 +0100 | [diff] [blame] | 119 | ALOGV("Setting counters to set %d for uid %d", counterSetNum, uid); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 120 | |
| 121 | snprintf(lineBuf, sizeof(lineBuf), "s %d %d", counterSetNum, uid); |
| 122 | res = write_ctrl(lineBuf); |
| 123 | return res; |
| 124 | } |
| 125 | |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 126 | int legacy_deleteTagData(int tag, uid_t uid) { |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 127 | char lineBuf[CTRL_MAX_INPUT_LEN]; |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 128 | int cnt = 0, res = 0; |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 129 | uint64_t kTag = (uint64_t)tag << 32; |
| 130 | |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 131 | ALOGV("Deleting tag data with tag %" PRIx64 "{%d,0} for uid %d", kTag, tag, uid); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 132 | |
Chenbo Feng | baede73 | 2017-10-25 12:31:43 -0700 | [diff] [blame] | 133 | pthread_once(&resTrackInitDone, legacy_resTrack); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 134 | |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 135 | snprintf(lineBuf, sizeof(lineBuf), "d %" PRIu64 " %d", kTag, uid); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 136 | res = write_ctrl(lineBuf); |
| 137 | if (res < 0) { |
Mark Salyzyn | 0425b64 | 2014-03-07 15:08:20 -0800 | [diff] [blame] | 138 | ALOGI("Deleting tag data with tag %" PRIx64 "/%d for uid %d failed with cnt=%d errno=%d", |
Chenbo Feng | 5b11831 | 2017-10-25 11:23:50 -0700 | [diff] [blame] | 139 | kTag, tag, uid, cnt, errno); |
JP Abgrall | 243123f | 2011-09-09 15:02:08 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return res; |
| 143 | } |