1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* -*- linux-c -*-
* Utility functions for handling file systems and mount namespaces.
*
* Copyright (C) 2023 by OpenResty Inc.
* This file is part of systemtap, and is free software. You can
* redistribute it and/or modify it under the terms of the GNU General
* Public License (GPL); either version 2, or (at your option) any
* later version.
*/
#ifndef LINUX_STAP_FS_H
#define LINUX_STAP_FS_H
#include <linux/namei.h>
#include <linux/fs_struct.h>
#include <linux/nsproxy.h>
#include <linux/proc_ns.h>
static int _stp_target_mnt_ns_fd = -1;
static int _stp_orig_mnt_ns_fd = -1;
static bool _stp_fs_struct_unshared = false;
static inline bool
has_set_mnt_ns(void)
{
static int cached_res = -1;
if (cached_res != -1)
return cached_res;
cached_res =
(kallsyms_proc_ns_file && kallsyms_unshare_nsproxy_namespaces
&& kallsyms_switch_task_namespaces && kallsyms_free_nsproxy)
;
return cached_res;
}
static inline int
stap_set_mnt_ns(int fd)
{
#ifdef STAPCONF_NSSET_COMPLETE
struct file *file;
struct nsset nsset = {};
struct ns_common *ns = NULL;
int err = 0;
struct task_struct *me;
if (!kallsyms_proc_ns_file || !kallsyms_unshare_nsproxy_namespaces
|| !kallsyms_switch_task_namespaces || !kallsyms_free_nsproxy)
{
/* NB do nothing; ignore the error when the kernel is too
* old to support this. */
return 0;
}
dbug_task(2, "using our own setns() impl instead of the syscall\n");
me = current;
file = fget(fd);
if (!file)
return -EBADF;
/* unlike the setns() syscall, we don't allow non-proc ns file */
if (!(ibt_wrapper(bool,
(*(proc_ns_file_fn) kallsyms_proc_ns_file)(file))))
err = -EINVAL;
if (unlikely(err))
goto out;
ns = get_proc_ns(file_inode(file));
if (ns->ops->type != CLONE_NEWNS) {
err = -EINVAL;
goto out;
}
/* NB: Alas. we actually need to call create_nsproxy_namesapes() here
* but it is an internal symbol declared by 'static'. The
* unshare_nsproxy_namespaces() function is the closest symbol we can
* use; but it allocates a new mnt ns we don't need. */
err = ibt_wrapper(int, (*(unshare_nsproxy_namespaces_fn)
kallsyms_unshare_nsproxy_namespaces)(CLONE_NEWNS, &nsset.nsproxy,
NULL, NULL));
if (unlikely(err)) {
goto out;
}
if (unlikely(nsset.nsproxy == NULL)) {
err = -ENOMEM;
goto out;
}
nsset.cred = current_cred();
if (unlikely(!nsset.cred)) {
err = -ENOMEM;
goto out0;
}
nsset.fs = me->fs;
nsset.flags = CLONE_NEWNS;
err = ns->ops->install(&nsset, ns);
if (unlikely(!err)) {
/* transfer ownership */
void_ibt_wrapper((*(switch_task_namespaces_fn) kallsyms_switch_task_namespaces)(me, nsset.nsproxy));
nsset.nsproxy = NULL;
}
out0:
if (nsset.nsproxy)
void_ibt_wrapper((*(free_nsproxy_fn) kallsyms_free_nsproxy)(nsset.nsproxy));
out:
fput(file);
return err;
#else /* !defined(STAPCONF_NSSET_COMPLETE) */
/* NB do nothing; ignore the error when the kernel is too
* old to support this. */
return 0;
#endif
}
/* Returns 0 on success or an error code otherwise. */
static inline int
stap_switch_to_target_mnt_ns_if_needed(bool *switched_ptr)
{
if (_stp_target_mnt_ns_fd > 0 && has_set_mnt_ns()) {
bool just_unshared = false;
int rc;
if (unlikely(! _stp_fs_struct_unshared)) {
rc = unshare_fs_struct();
if (unlikely(rc != 0)) {
_stp_error("unshare_fs_struct() failed: %d\n",
rc);
return rc;
}
_stp_fs_struct_unshared = true;
just_unshared = true;
}
dbug_task(2, "switching mount namespace to the target process's for task '%s'\n",
current ? current->comm : "<null>");
rc = stap_set_mnt_ns(_stp_target_mnt_ns_fd);
if (unlikely(rc == -EINVAL && ! just_unshared)) {
/* setns() is per-thread so it's still possible for
* a new thread to need a call to unshare_fs_struct() */
if (unlikely(! _stp_fs_struct_unshared)) {
rc = unshare_fs_struct();
if (unlikely(rc != 0)) {
_stp_error("unshare_fs_struct() failed: %d\n",
rc);
return rc;
}
_stp_fs_struct_unshared = true;
}
rc = stap_set_mnt_ns(_stp_target_mnt_ns_fd);
}
if (unlikely(rc != 0)) {
_stp_error("setns(%d) failed for the target mount namespace: %d\n",
_stp_target_mnt_ns_fd, rc);
return rc;
}
*switched_ptr = true;
}
return 0;
}
static inline int
stap_switch_to_orig_mnt_ns_if_needed(void)
{
if (likely(_stp_orig_mnt_ns_fd > 0 && has_set_mnt_ns())) {
int rc;
dbug_task(2, "switching mount namespace to the original one for task '%s'\n",
current ? current->comm : "<null>");
rc = stap_set_mnt_ns(_stp_orig_mnt_ns_fd);
if (unlikely(rc != 0)) {
_stp_error("setns(%d) failed for the original mount namespace: %d\n",
_stp_target_mnt_ns_fd, rc);
return rc;
}
}
return 0;
}
static inline char *
stap_real_path(const char *pathname, size_t *len_ptr)
{
int rc;
char *ret;
char *path_buf = NULL;
char *p;
struct path path;
bool mnt_ns_switched = false;
might_sleep();
*len_ptr = 0;
path_buf = _stp_kmalloc(PATH_MAX);
if (unlikely(path_buf == NULL)) {
ret = ERR_PTR(-ENOMEM);
goto out;
}
rc = stap_switch_to_target_mnt_ns_if_needed(&mnt_ns_switched);
if (unlikely(rc != 0)) {
ret = ERR_PTR(rc);
goto out_with_buf;
}
rc = kern_path(pathname, LOOKUP_FOLLOW, &path);
if (mnt_ns_switched) {
int rc;
rc = stap_switch_to_orig_mnt_ns_if_needed();
if (unlikely(rc != 0)) {
ret = ERR_PTR(rc);
goto out_with_buf;
}
}
if (unlikely(rc != 0)) {
_stp_error("Couldn't resolve target program file path '%s': %d\n",
pathname, rc);
ret = ERR_PTR(rc);
goto out_with_buf;
}
p = d_path(&path, path_buf, PATH_MAX);
if (unlikely(p == NULL)) {
ret = ERR_PTR(-EINVAL);
goto out_with_path;
}
if (unlikely(IS_ERR(p))) {
_stp_error("d_path() failed for path '%s': %ld\n", pathname,
PTR_ERR(p));
ret = p;
goto out_with_path;
}
#if 0
pr_warn("path buf: '%s' -> '%s'\n", pathname, p);
#endif
if (strcmp(p, pathname) != 0) { /* found a symlink */
char *q;
size_t len = strlen(p);
if (unlikely(len == 0)) {
ret = ERR_PTR(-EINVAL);
goto out_with_path;
}
q = _stp_kmalloc(len + 1);
if (unlikely(q == NULL)) {
ret = ERR_PTR(-ENOMEM);
goto out_with_path;
}
memcpy(q, p, len + 1);
*len_ptr = len;
ret = q;
goto out_with_path;
}
ret = NULL;
out_with_path:
path_put(&path);
out_with_buf:
if (likely(path_buf != NULL))
_stp_kfree(path_buf);
out:
return ret;
}
#endif /* LINUX_STAP_FS_H */
|