blob: 948d09c6057e3e622e9a78291665bb6c304073e9 [file] [log] [blame]
jeffhao2b8f76c2011-05-05 14:25:36 -07001/*
2 * Copyright (C) 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
Mark Salyzyncfd5b082016-10-17 14:28:00 -070017#define LOG_TAG "socket-unix"
18
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/socket.h>
David Pursellb34e4a02016-02-01 09:42:09 -080023#include <sys/uio.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080024#include <sys/un.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070025#include <time.h>
26#include <unistd.h>
David Pursellb34e4a02016-02-01 09:42:09 -080027
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070028#include <android/log.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080029#include <cutils/android_get_control_file.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070030#include <cutils/sockets.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070031
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080032#include "android_get_control_env.h"
33
Elliott Hughes9b828ad2015-07-30 08:47:35 -070034#if defined(__ANDROID__)
jeffhao2b8f76c2011-05-05 14:25:36 -070035/* For the socket trust (credentials) check */
36#include <private/android_filesystem_config.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070037#define __android_unused
38#else
39#define __android_unused __attribute__((__unused__))
jeffhao2b8f76c2011-05-05 14:25:36 -070040#endif
41
David Pursellb34e4a02016-02-01 09:42:09 -080042bool socket_peer_is_trusted(int fd __android_unused) {
Elliott Hughes9b828ad2015-07-30 08:47:35 -070043#if defined(__ANDROID__)
David Pursellb34e4a02016-02-01 09:42:09 -080044 ucred cr;
jeffhao2b8f76c2011-05-05 14:25:36 -070045 socklen_t len = sizeof(cr);
46 int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
47
48 if (n != 0) {
Steve Block01dda202012-01-06 14:13:42 +000049 ALOGE("could not get socket credentials: %s\n", strerror(errno));
jeffhao2b8f76c2011-05-05 14:25:36 -070050 return false;
51 }
52
53 if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
Steve Block01dda202012-01-06 14:13:42 +000054 ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
jeffhao2b8f76c2011-05-05 14:25:36 -070055 return false;
56 }
57#endif
58
59 return true;
60}
David Pursell0eb8e1b2016-01-14 17:18:27 -080061
62int socket_close(int sock) {
David Pursell8385fb22016-01-29 13:49:25 -080063 return close(sock);
David Pursell0eb8e1b2016-01-14 17:18:27 -080064}
David Pursell572bce22016-01-15 14:19:56 -080065
66int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
David Pursellb34e4a02016-02-01 09:42:09 -080067 timeval tv;
David Pursell8385fb22016-01-29 13:49:25 -080068 tv.tv_sec = timeout_ms / 1000;
69 tv.tv_usec = (timeout_ms % 1000) * 1000;
70 return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
71}
72
David Pursell8385fb22016-01-29 13:49:25 -080073ssize_t socket_send_buffers(cutils_socket_t sock,
David Pursellb34e4a02016-02-01 09:42:09 -080074 const cutils_socket_buffer_t* buffers,
David Pursell8385fb22016-01-29 13:49:25 -080075 size_t num_buffers) {
David Pursellb34e4a02016-02-01 09:42:09 -080076 if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) {
77 return -1;
78 }
79
80 iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS];
81 for (size_t i = 0; i < num_buffers; ++i) {
82 // It's safe to cast away const here; iovec declares non-const
83 // void* because it's used for both send and receive, but since
84 // we're only sending, the data won't be modified.
85 iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data);
86 iovec_buffers[i].iov_len = buffers[i].length;
87 }
88
89 return writev(sock, iovec_buffers, num_buffers);
David Pursell572bce22016-01-15 14:19:56 -080090}
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080091
92int android_get_control_socket(const char* name) {
93 int fd = __android_get_control_from_env(ANDROID_SOCKET_ENV_PREFIX, name);
94
95 if (fd < 0) return fd;
96
97 // Compare to UNIX domain socket name, must match!
98 struct sockaddr_un addr;
99 socklen_t addrlen = sizeof(addr);
100 int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen));
101 if (ret < 0) return -1;
102 char *path = NULL;
103 if (asprintf(&path, ANDROID_SOCKET_DIR "/%s", name) < 0) return -1;
104 if (!path) return -1;
105 int cmp = strcmp(addr.sun_path, path);
106 free(path);
107 if (cmp != 0) return -1;
108
109 // It is what we think it is
110 return fd;
111}