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