| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/sockets.h> | 
|  | 18 |  | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 19 | #define LOG_TAG "socket-unix" | 
|  | 20 |  | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 21 | #include <stdio.h> | 
|  | 22 | #include <stdlib.h> | 
|  | 23 | #include <string.h> | 
|  | 24 | #include <sys/socket.h> | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 25 | #include <sys/uio.h> | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 26 | #include <sys/un.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 27 | #include <time.h> | 
|  | 28 | #include <unistd.h> | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 29 |  | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 30 | #include <cutils/android_get_control_file.h> | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 31 | #include <log/log.h> | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 32 |  | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 33 | #include "android_get_control_env.h" | 
|  | 34 |  | 
| Mark Salyzyn | 0958b18 | 2016-11-17 08:33:19 -0800 | [diff] [blame] | 35 | #ifndef TEMP_FAILURE_RETRY | 
|  | 36 | #define TEMP_FAILURE_RETRY(exp) (exp) // KISS implementation | 
|  | 37 | #endif | 
|  | 38 |  | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 39 | #if defined(__ANDROID__) | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 40 | /* For the socket trust (credentials) check */ | 
|  | 41 | #include <private/android_filesystem_config.h> | 
| Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 42 | #define __android_unused | 
|  | 43 | #else | 
|  | 44 | #define __android_unused __attribute__((__unused__)) | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 45 | #endif | 
|  | 46 |  | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 47 | bool socket_peer_is_trusted(int fd __android_unused) { | 
| Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 48 | #if defined(__ANDROID__) | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 49 | ucred cr; | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 50 | socklen_t len = sizeof(cr); | 
|  | 51 | int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); | 
|  | 52 |  | 
|  | 53 | if (n != 0) { | 
| Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 54 | ALOGE("could not get socket credentials: %s\n", strerror(errno)); | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 55 | return false; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) { | 
| Steve Block | 01dda20 | 2012-01-06 14:13:42 +0000 | [diff] [blame] | 59 | ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid); | 
| jeffhao | 2b8f76c | 2011-05-05 14:25:36 -0700 | [diff] [blame] | 60 | return false; | 
|  | 61 | } | 
|  | 62 | #endif | 
|  | 63 |  | 
|  | 64 | return true; | 
|  | 65 | } | 
| David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 66 |  | 
|  | 67 | int socket_close(int sock) { | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 68 | return close(sock); | 
| David Pursell | 0eb8e1b | 2016-01-14 17:18:27 -0800 | [diff] [blame] | 69 | } | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 70 |  | 
|  | 71 | int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) { | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 72 | timeval tv; | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 73 | tv.tv_sec = timeout_ms / 1000; | 
|  | 74 | tv.tv_usec = (timeout_ms % 1000) * 1000; | 
|  | 75 | return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | 
|  | 76 | } | 
|  | 77 |  | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 78 | ssize_t socket_send_buffers(cutils_socket_t sock, | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 79 | const cutils_socket_buffer_t* buffers, | 
| David Pursell | 8385fb2 | 2016-01-29 13:49:25 -0800 | [diff] [blame] | 80 | size_t num_buffers) { | 
| David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 81 | if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) { | 
|  | 82 | return -1; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS]; | 
|  | 86 | for (size_t i = 0; i < num_buffers; ++i) { | 
|  | 87 | // It's safe to cast away const here; iovec declares non-const | 
|  | 88 | // void* because it's used for both send and receive, but since | 
|  | 89 | // we're only sending, the data won't be modified. | 
|  | 90 | iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data); | 
|  | 91 | iovec_buffers[i].iov_len = buffers[i].length; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | return writev(sock, iovec_buffers, num_buffers); | 
| David Pursell | 572bce2 | 2016-01-15 14:19:56 -0800 | [diff] [blame] | 95 | } | 
| Mark Salyzyn | 52bd37e | 2016-11-07 09:39:30 -0800 | [diff] [blame] | 96 |  | 
|  | 97 | int android_get_control_socket(const char* name) { | 
|  | 98 | int fd = __android_get_control_from_env(ANDROID_SOCKET_ENV_PREFIX, name); | 
|  | 99 |  | 
|  | 100 | if (fd < 0) return fd; | 
|  | 101 |  | 
|  | 102 | // Compare to UNIX domain socket name, must match! | 
|  | 103 | struct sockaddr_un addr; | 
|  | 104 | socklen_t addrlen = sizeof(addr); | 
|  | 105 | int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen)); | 
|  | 106 | if (ret < 0) return -1; | 
|  | 107 | char *path = NULL; | 
|  | 108 | if (asprintf(&path, ANDROID_SOCKET_DIR "/%s", name) < 0) return -1; | 
|  | 109 | if (!path) return -1; | 
|  | 110 | int cmp = strcmp(addr.sun_path, path); | 
|  | 111 | free(path); | 
|  | 112 | if (cmp != 0) return -1; | 
|  | 113 |  | 
|  | 114 | // It is what we think it is | 
|  | 115 | return fd; | 
|  | 116 | } |