|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2021 Google, Inc. All rights reserved. |
| 4 | + * $COPYRIGHT$ |
| 5 | + * |
| 6 | + * Additional copyrights may follow |
| 7 | + * |
| 8 | + * $HEADER$ |
| 9 | + */ |
| 10 | +#include "opal_config.h" |
| 11 | + |
| 12 | +#include "opal/mca/smsc/base/base.h" |
| 13 | +#include "opal/mca/smsc/cma/smsc_cma_internal.h" |
| 14 | + |
| 15 | +#include <fcntl.h> |
| 16 | +#include <stdio.h> |
| 17 | +#include <sys/prctl.h> |
| 18 | +#include <sys/stat.h> |
| 19 | +#include <sys/types.h> |
| 20 | +#include <unistd.h> |
| 21 | + |
| 22 | +static int mca_smsc_cma_component_register(void); |
| 23 | +static int mca_smsc_cma_component_open(void); |
| 24 | +static int mca_smsc_cma_component_close(void); |
| 25 | +static int mca_smsc_cma_component_query(void); |
| 26 | +static mca_smsc_module_t *mca_smsc_cma_component_enable(void); |
| 27 | + |
| 28 | +#define MCA_SMSC_CMA_DEFAULT_PRIORITY 37 |
| 29 | +static const int mca_smsc_cma_default_priority = MCA_SMSC_CMA_DEFAULT_PRIORITY; |
| 30 | + |
| 31 | +mca_smsc_component_t mca_smsc_cma_component = { |
| 32 | + .smsc_version = { |
| 33 | + MCA_SMSC_DEFAULT_VERSION("cma"), |
| 34 | + .mca_open_component = mca_smsc_cma_component_open, |
| 35 | + .mca_close_component = mca_smsc_cma_component_close, |
| 36 | + .mca_register_component_params = mca_smsc_cma_component_register, |
| 37 | + }, |
| 38 | + .priority = MCA_SMSC_CMA_DEFAULT_PRIORITY, |
| 39 | + .query = mca_smsc_cma_component_query, |
| 40 | + .enable = mca_smsc_cma_component_enable, |
| 41 | +}; |
| 42 | + |
| 43 | +static int mca_smsc_cma_component_register(void) |
| 44 | +{ |
| 45 | + mca_smsc_base_register_default_params(&mca_smsc_cma_component, mca_smsc_cma_default_priority); |
| 46 | + return OPAL_SUCCESS; |
| 47 | +} |
| 48 | + |
| 49 | +static int mca_smsc_cma_component_open(void) |
| 50 | +{ |
| 51 | + /* nothing to do */ |
| 52 | + return OPAL_SUCCESS; |
| 53 | +} |
| 54 | + |
| 55 | +static int mca_smsc_cma_component_close(void) |
| 56 | +{ |
| 57 | + /* nothing to do */ |
| 58 | + return OPAL_SUCCESS; |
| 59 | +} |
| 60 | + |
| 61 | +/* |
| 62 | + * mca_btl_sm_parse_proc_ns_user() tries to get the user namespace ID |
| 63 | + * of the current process. |
| 64 | + * Returns the ID of the user namespace. In the case of an error '0' is returned. |
| 65 | + */ |
| 66 | +ino_t mca_smsc_cma_get_user_ns_id(void) |
| 67 | +{ |
| 68 | + struct stat buf; |
| 69 | + |
| 70 | + if (0 > stat("/proc/self/ns/user", &buf)) { |
| 71 | + /* |
| 72 | + * Something went wrong, probably an old kernel that does not support namespaces |
| 73 | + * simply assume all processes are in the same user namespace and return 0 |
| 74 | + */ |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + return buf.st_ino; |
| 79 | +} |
| 80 | + |
| 81 | +static int mca_smsc_cma_send_modex(void) |
| 82 | +{ |
| 83 | + mca_smsc_cma_modex_t modex; |
| 84 | + |
| 85 | + modex.pid = getpid(); |
| 86 | + modex.user_ns_id = mca_smsc_cma_get_user_ns_id(); |
| 87 | + |
| 88 | + int rc; |
| 89 | + OPAL_MODEX_SEND(rc, PMIX_LOCAL, &mca_smsc_cma_component.smsc_version, &modex, sizeof(modex)); |
| 90 | + return rc; |
| 91 | +} |
| 92 | + |
| 93 | +static int mca_smsc_cma_component_query(void) |
| 94 | +{ |
| 95 | + /* Check if we have the proper permissions for CMA */ |
| 96 | + char buffer = '0'; |
| 97 | + bool cma_happy = false; |
| 98 | + |
| 99 | + /* check system setting for current ptrace scope */ |
| 100 | + int fd = open("/proc/sys/kernel/yama/ptrace_scope", O_RDONLY); |
| 101 | + if (0 <= fd) { |
| 102 | + int ret = read(fd, &buffer, 1); |
| 103 | + if (ret < 0) { |
| 104 | + opal_output_verbose(MCA_BASE_VERBOSE_COMPONENT, |
| 105 | + opal_smsc_base_framework.framework_output, |
| 106 | + "mca_smsc_cma_component_query: could not read ptrace_scope. " |
| 107 | + "assuming ptrace scope is 0"); |
| 108 | + } |
| 109 | + close(fd); |
| 110 | + } |
| 111 | + |
| 112 | + /* ptrace scope 0 will allow an attach from any of the process owner's |
| 113 | + * processes. ptrace scope 1 limits attachers to the process tree |
| 114 | + * starting at the parent of this process. */ |
| 115 | + if ('0' != buffer) { |
| 116 | +#if defined PR_SET_PTRACER |
| 117 | + /* try setting the ptrace scope to allow attach */ |
| 118 | + int ret = prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0); |
| 119 | + if (0 == ret) { |
| 120 | + cma_happy = true; |
| 121 | + } |
| 122 | +#endif |
| 123 | + } else { |
| 124 | + cma_happy = true; |
| 125 | + } |
| 126 | + |
| 127 | + if (!cma_happy) { |
| 128 | + opal_output_verbose(MCA_BASE_VERBOSE_COMPONENT, opal_smsc_base_framework.framework_output, |
| 129 | + "mca_smsc_cma_component_query: could not select for use. insufficient " |
| 130 | + "ptrace permissions."); |
| 131 | + mca_smsc_cma_component.priority = -1; |
| 132 | + return OPAL_ERR_NOT_AVAILABLE; |
| 133 | + } |
| 134 | + |
| 135 | + mca_smsc_cma_send_modex(); |
| 136 | + |
| 137 | + return OPAL_SUCCESS; |
| 138 | +} |
| 139 | + |
| 140 | +static mca_smsc_module_t *mca_smsc_cma_component_enable(void) |
| 141 | +{ |
| 142 | + if (0 > mca_smsc_cma_component.priority) { |
| 143 | + return NULL; |
| 144 | + } |
| 145 | + |
| 146 | + return &mca_smsc_cma_module; |
| 147 | +} |
0 commit comments