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
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2021 Google, Inc. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include "opal/mca/smsc/base/base.h"
#include "opal/mca/smsc/cma/smsc_cma_internal.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static int mca_smsc_cma_component_register(void);
static int mca_smsc_cma_component_open(void);
static int mca_smsc_cma_component_close(void);
static int mca_smsc_cma_component_query(void);
static mca_smsc_module_t *mca_smsc_cma_component_enable(void);
#define MCA_SMSC_CMA_DEFAULT_PRIORITY 37
static const int mca_smsc_cma_default_priority = MCA_SMSC_CMA_DEFAULT_PRIORITY;
mca_smsc_component_t mca_smsc_cma_component = {
.smsc_version = {
MCA_SMSC_DEFAULT_VERSION("cma"),
.mca_open_component = mca_smsc_cma_component_open,
.mca_close_component = mca_smsc_cma_component_close,
.mca_register_component_params = mca_smsc_cma_component_register,
},
.priority = MCA_SMSC_CMA_DEFAULT_PRIORITY,
.query = mca_smsc_cma_component_query,
.enable = mca_smsc_cma_component_enable,
};
static int mca_smsc_cma_component_register(void)
{
mca_smsc_base_register_default_params(&mca_smsc_cma_component, mca_smsc_cma_default_priority);
return OPAL_SUCCESS;
}
static int mca_smsc_cma_component_open(void)
{
/* nothing to do */
return OPAL_SUCCESS;
}
static int mca_smsc_cma_component_close(void)
{
/* nothing to do */
return OPAL_SUCCESS;
}
/*
* mca_btl_sm_parse_proc_ns_user() tries to get the user namespace ID
* of the current process.
* Returns the ID of the user namespace. In the case of an error '0' is returned.
*/
ino_t mca_smsc_cma_get_user_ns_id(void)
{
struct stat buf;
if (0 > stat("/proc/self/ns/user", &buf)) {
/*
* Something went wrong, probably an old kernel that does not support namespaces
* simply assume all processes are in the same user namespace and return 0
*/
return 0;
}
return buf.st_ino;
}
static int mca_smsc_cma_send_modex(void)
{
mca_smsc_cma_modex_t modex;
modex.pid = getpid();
modex.user_ns_id = mca_smsc_cma_get_user_ns_id();
int rc;
OPAL_MODEX_SEND(rc, PMIX_LOCAL, &mca_smsc_cma_component.smsc_version, &modex, sizeof(modex));
return rc;
}
static int mca_smsc_cma_component_query(void)
{
/* Check if we have the proper permissions for CMA */
char buffer = '0';
bool cma_happy = false;
/* check system setting for current ptrace scope */
int fd = open("/proc/sys/kernel/yama/ptrace_scope", O_RDONLY);
if (0 <= fd) {
int ret = read(fd, &buffer, 1);
if (ret < 0) {
opal_output_verbose(MCA_BASE_VERBOSE_COMPONENT,
opal_smsc_base_framework.framework_output,
"mca_smsc_cma_component_query: could not read ptrace_scope. "
"assuming ptrace scope is 0");
}
close(fd);
}
/* ptrace scope 0 will allow an attach from any of the process owner's
* processes. ptrace scope 1 limits attachers to the process tree
* starting at the parent of this process. */
if ('0' != buffer) {
#if defined PR_SET_PTRACER
/* try setting the ptrace scope to allow attach */
int ret = prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
if (0 == ret) {
cma_happy = true;
}
#endif
} else {
cma_happy = true;
}
if (!cma_happy) {
opal_output_verbose(MCA_BASE_VERBOSE_COMPONENT, opal_smsc_base_framework.framework_output,
"mca_smsc_cma_component_query: could not select for use. insufficient "
"ptrace permissions.");
mca_smsc_cma_component.priority = -1;
return OPAL_ERR_NOT_AVAILABLE;
}
mca_smsc_cma_send_modex();
return OPAL_SUCCESS;
}
static mca_smsc_module_t *mca_smsc_cma_component_enable(void)
{
if (0 > mca_smsc_cma_component.priority) {
return NULL;
}
return &mca_smsc_cma_module;
}
|