|
| 1 | +/* Copyright 2025 HAProxy Technologies LLC */ |
| 2 | +/* */ |
| 3 | +/* Licensed under the Apache License, Version 2.0 (the "License"); */ |
| 4 | +/* you may not use this file except in compliance with the License. */ |
| 5 | +/* You may obtain a copy of the License at */ |
| 6 | +/* */ |
| 7 | +/* http://www.apache.org/licenses/LICENSE-2.0 */ |
| 8 | +/* */ |
| 9 | +/* Unless required by applicable law or agreed to in writing, software */ |
| 10 | +/* distributed under the License is distributed on an "AS IS" BASIS, */ |
| 11 | +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ |
| 12 | +/* See the License for the specific language governing permissions and */ |
| 13 | +/* limitations under the License. */ |
| 14 | + |
| 15 | +#include <dlfcn.h> |
| 16 | +#include <errno.h> |
| 17 | +#include <fcntl.h> |
| 18 | +#include <limits.h> |
| 19 | +#include <stdarg.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <string.h> |
| 23 | +#include <sys/stat.h> |
| 24 | +#include <sys/types.h> |
| 25 | +#include <unistd.h> |
| 26 | + |
| 27 | +#define PATH_MAX 4096 |
| 28 | +#define BLOCKED_PATH "/var/run/secrets/kubernetes.io/" |
| 29 | + |
| 30 | +static char canonical_blocked[PATH_MAX] = {0}; |
| 31 | +static size_t canonical_blocked_len = 0; |
| 32 | + |
| 33 | +__attribute__((constructor)) static void init_blocked_path() { |
| 34 | + if (!realpath(BLOCKED_PATH, canonical_blocked)) { |
| 35 | + strncpy(canonical_blocked, BLOCKED_PATH, PATH_MAX - 1); |
| 36 | + canonical_blocked[PATH_MAX - 1] = '\0'; |
| 37 | + } |
| 38 | + canonical_blocked_len = strlen(canonical_blocked); |
| 39 | +} |
| 40 | + |
| 41 | +__attribute__((always_inline)) inline static int |
| 42 | +is_blocked(const char *pathname) { |
| 43 | + char resolved[PATH_MAX]; |
| 44 | + const char *target = pathname; |
| 45 | + |
| 46 | + if (realpath(pathname, resolved)) { |
| 47 | + target = resolved; |
| 48 | + } |
| 49 | + |
| 50 | + return strncmp(target, canonical_blocked, canonical_blocked_len) == 0; |
| 51 | +} |
| 52 | + |
| 53 | +static int (*real_open)(const char *, int, ...) = NULL; |
| 54 | +static int (*real_open64)(const char *, int, ...) = NULL; |
| 55 | +static FILE *(*real_fopen)(const char *, const char *) = NULL; |
| 56 | +static FILE *(*real_fopen64)(const char *, const char *) = NULL; |
| 57 | +static FILE *(*real_freopen)(const char *, const char *, FILE *) = NULL; |
| 58 | +static FILE *(*real_freopen64)(const char *, const char *, FILE *) = NULL; |
| 59 | + |
| 60 | +__attribute__((constructor)) static void init_hooks() { |
| 61 | + real_open = dlsym(RTLD_NEXT, "open"); |
| 62 | + real_open64 = dlsym(RTLD_NEXT, "open64"); |
| 63 | + real_fopen = dlsym(RTLD_NEXT, "fopen"); |
| 64 | + real_fopen64 = dlsym(RTLD_NEXT, "fopen64"); |
| 65 | + real_freopen = dlsym(RTLD_NEXT, "freopen"); |
| 66 | + real_freopen64 = dlsym(RTLD_NEXT, "freopen64"); |
| 67 | +} |
| 68 | + |
| 69 | +int open(const char *pathname, int flags, ...) { |
| 70 | + if (is_blocked(pathname)) { |
| 71 | + errno = EACCES; |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + va_list args; |
| 76 | + va_start(args, flags); |
| 77 | + int fd; |
| 78 | + if (flags & O_CREAT) { |
| 79 | + mode_t mode = (mode_t)va_arg(args, int); |
| 80 | + fd = real_open(pathname, flags, mode); |
| 81 | + } else { |
| 82 | + fd = real_open(pathname, flags); |
| 83 | + } |
| 84 | + va_end(args); |
| 85 | + return fd; |
| 86 | +} |
| 87 | + |
| 88 | +int open64(const char *pathname, int flags, ...) { |
| 89 | + if (is_blocked(pathname)) { |
| 90 | + errno = EACCES; |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + va_list args; |
| 95 | + va_start(args, flags); |
| 96 | + int fd; |
| 97 | + if (flags & O_CREAT) { |
| 98 | + mode_t mode = (mode_t)va_arg(args, int); |
| 99 | + fd = real_open64(pathname, flags, mode); |
| 100 | + } else { |
| 101 | + fd = real_open64(pathname, flags); |
| 102 | + } |
| 103 | + va_end(args); |
| 104 | + return fd; |
| 105 | +} |
| 106 | + |
| 107 | +FILE *fopen(const char *pathname, const char *mode) { |
| 108 | + if (is_blocked(pathname)) { |
| 109 | + errno = EACCES; |
| 110 | + return NULL; |
| 111 | + } |
| 112 | + return real_fopen(pathname, mode); |
| 113 | +} |
| 114 | + |
| 115 | +FILE *fopen64(const char *pathname, const char *mode) { |
| 116 | + if (is_blocked(pathname)) { |
| 117 | + errno = EACCES; |
| 118 | + return NULL; |
| 119 | + } |
| 120 | + return real_fopen64(pathname, mode); |
| 121 | +} |
| 122 | + |
| 123 | +FILE *freopen(const char *pathname, const char *mode, FILE *stream) { |
| 124 | + if (is_blocked(pathname)) { |
| 125 | + errno = EACCES; |
| 126 | + return NULL; |
| 127 | + } |
| 128 | + return real_freopen(pathname, mode, stream); |
| 129 | +} |
| 130 | + |
| 131 | +FILE *freopen64(const char *pathname, const char *mode, FILE *stream) { |
| 132 | + if (is_blocked(pathname)) { |
| 133 | + errno = EACCES; |
| 134 | + return NULL; |
| 135 | + } |
| 136 | + return real_freopen64(pathname, mode, stream); |
| 137 | +} |
0 commit comments