blob: 0cb8a4dc52b4a59815efcac431e755efeb75040d [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
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/sockets.h>
18
Mark Salyzyncfd5b082016-10-17 14:28:00 -070019#define LOG_TAG "socket-unix"
20
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
David Pursellb34e4a02016-02-01 09:42:09 -080025#include <sys/uio.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080026#include <sys/un.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070027#include <time.h>
28#include <unistd.h>
David Pursellb34e4a02016-02-01 09:42:09 -080029
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080030#include <cutils/android_get_control_file.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080031#include <log/log.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070032
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080033#include "android_get_control_env.h"
34
Elliott Hughes9b828ad2015-07-30 08:47:35 -070035#if defined(__ANDROID__)
jeffhao2b8f76c2011-05-05 14:25:36 -070036/* For the socket trust (credentials) check */
37#include <private/android_filesystem_config.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070038#define __android_unused
39#else
40#define __android_unused __attribute__((__unused__))
jeffhao2b8f76c2011-05-05 14:25:36 -070041#endif
42
David Pursellb34e4a02016-02-01 09:42:09 -080043bool socket_peer_is_trusted(int fd __android_unused) {
Elliott Hughes9b828ad2015-07-30 08:47:35 -070044#if defined(__ANDROID__)
David Pursellb34e4a02016-02-01 09:42:09 -080045 ucred cr;
jeffhao2b8f76c2011-05-05 14:25:36 -070046 socklen_t len = sizeof(cr);
47 int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
48
49 if (n != 0) {
Steve Block01dda202012-01-06 14:13:42 +000050 ALOGE("could not get socket credentials: %s\n", strerror(errno));
jeffhao2b8f76c2011-05-05 14:25:36 -070051 return false;
52 }
53
54 if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
Steve Block01dda202012-01-06 14:13:42 +000055 ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
jeffhao2b8f76c2011-05-05 14:25:36 -070056 return false;
57 }
58#endif
59
60 return true;
61}
David Pursell0eb8e1b2016-01-14 17:18:27 -080062
63int socket_close(int sock) {
David Pursell8385fb22016-01-29 13:49:25 -080064 return close(sock);
David Pursell0eb8e1b2016-01-14 17:18:27 -080065}
David Pursell572bce22016-01-15 14:19:56 -080066
67int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
David Pursellb34e4a02016-02-01 09:42:09 -080068 timeval tv;
David Pursell8385fb22016-01-29 13:49:25 -080069 tv.tv_sec = timeout_ms / 1000;
70 tv.tv_usec = (timeout_ms % 1000) * 1000;
71 return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
72}
73
David Pursell8385fb22016-01-29 13:49:25 -080074ssize_t socket_send_buffers(cutils_socket_t sock,
David Pursellb34e4a02016-02-01 09:42:09 -080075 const cutils_socket_buffer_t* buffers,
David Pursell8385fb22016-01-29 13:49:25 -080076 size_t num_buffers) {
David Pursellb34e4a02016-02-01 09:42:09 -080077 if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) {
78 return -1;
79 }
80
81 iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS];
82 for (size_t i = 0; i < num_buffers; ++i) {
83 // It's safe to cast away const here; iovec declares non-const
84 // void* because it's used for both send and receive, but since
85 // we're only sending, the data won't be modified.
86 iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data);
87 iovec_buffers[i].iov_len = buffers[i].length;
88 }
89
90 return writev(sock, iovec_buffers, num_buffers);
David Pursell572bce22016-01-15 14:19:56 -080091}
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080092
93int android_get_control_socket(const char* name) {
94 int fd = __android_get_control_from_env(ANDROID_SOCKET_ENV_PREFIX, name);
95
96 if (fd < 0) return fd;
97
98 // Compare to UNIX domain socket name, must match!
99 struct sockaddr_un addr;
100 socklen_t addrlen = sizeof(addr);
Bernie Innocenti4351bb02018-05-28 14:56:57 +0900101 int ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen);
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800102 if (ret < 0) return -1;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800103
Bernie Innocenti4351bb02018-05-28 14:56:57 +0900104 constexpr char prefix[] = ANDROID_SOCKET_DIR "/";
105 constexpr size_t prefix_size = sizeof(prefix) - sizeof('\0');
106 if ((strncmp(addr.sun_path, prefix, prefix_size) == 0) &&
107 (strcmp(addr.sun_path + prefix_size, name) == 0)) {
108 // It is what we think it is
109 return fd;
110 }
111 return -1;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -0800112}