Bram Bonné | 95ca52a | 2020-12-03 19:03:55 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | // b/170214442: Intercept bind calls on NETLINK_ROUTE sockets and getifaddrs() calls. |
| 30 | // This entire file will be reverted before release. |
| 31 | |
| 32 | #include <async_safe/log.h> |
| 33 | #include <cutils/misc.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "bionic_appcompat.h" |
| 39 | |
| 40 | bool should_apply_soft_mac_restrictions(const char* const allowlist[]) { |
| 41 | if (getuid() < FIRST_APPLICATION_UID) { |
| 42 | // System app. No restrictions should be applied. |
| 43 | return false; |
| 44 | } |
| 45 | if (android_get_application_target_sdk_version() >= __ANDROID_API_R__) { |
| 46 | // Restrictions already applied by SELinux. Behave as normally. |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | char package_name[MAX_PACKAGE_NAME_LENGTH + 1]; |
| 51 | if (get_package_name(package_name, sizeof(package_name)) < 0) { |
| 52 | // Error in getting own package name. Apply restrictions by default. |
| 53 | async_safe_format_log(ANDROID_LOG_ERROR, "mac-restrictions", |
| 54 | "Could not determine own package name for uid %d", getuid()); |
| 55 | return true; |
| 56 | } |
| 57 | for (int i = 0; allowlist[i] != nullptr; i++) { |
| 58 | if (strcmp(package_name, allowlist[i]) == 0) { |
| 59 | async_safe_format_log(ANDROID_LOG_WARN, "mac-restrictions", |
| 60 | "Temporarily allowing %s to bypass MAC address restrictions.", |
| 61 | package_name); |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool should_apply_soft_mac_bind_restrictions() { |
| 69 | return should_apply_soft_mac_restrictions(soft_mac_bind_allowlist); |
| 70 | } |
| 71 | |
| 72 | bool should_apply_soft_mac_getlink_restrictions() { |
| 73 | return should_apply_soft_mac_restrictions(soft_mac_getlink_allowlist); |
| 74 | } |
| 75 | |
| 76 | int get_package_name(char* buffer, const int bufferlen) { |
| 77 | int file = open("/proc/self/cmdline", O_RDONLY | O_CLOEXEC); |
| 78 | if (file < 0) { |
| 79 | return file; |
| 80 | } |
| 81 | |
| 82 | ssize_t len = read(file, buffer, bufferlen - 1); |
| 83 | if (len < 0) { |
| 84 | close(file); |
| 85 | return -1; |
| 86 | } |
| 87 | buffer[len] = 0; |
| 88 | |
| 89 | close(file); |
| 90 | return 0; |
| 91 | } |