summaryrefslogtreecommitdiffstats
path: root/configd/src/apps/sentinel/platform-specific.cpp
blob: d1c3682e85720d2221ae45175894a3ed915c2647 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "platform-specific.h"
#include <vespa/vespalib/util/error.h>
#include <cstdlib>
#include <string_view>
#ifdef __linux__
#include <sys/prctl.h>
#endif

#include <vespa/log/log.h>
LOG_SETUP(".sentinel.platform-specific");

using namespace std::string_view_literals;

namespace config::platform_specific {

namespace {

[[maybe_unused]] [[nodiscard]]
bool is_env_toggled(const char* var_name) {
    const char* maybe_toggled = getenv(var_name);
    return (maybe_toggled && (maybe_toggled == "true"sv || maybe_toggled == "yes"sv));
}

}

void pledge_no_new_privileges_if_env_configured() {
#ifdef __linux__
    if (is_env_toggled("VESPA_PR_SET_NO_NEW_PRIVS")) {
        // One-way toggle to prevent any subprocess from possibly getting extra privileges via
        // setuid/setgid executables (modulo exciting things like kernel bugs or a small, trained
        // rat that climbs into your computer and pulls an adorably tiny lever labeled "root access").
        // Helps mitigate a certain class of vulnerabilities, and also allows processes to install
        // their own seccomp filters.
        // See https://www.kernel.org/doc/Documentation/prctl/no_new_privs.txt
        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
            LOG(warning, "Failed to invoke prctl(PR_SET_NO_NEW_PRIVS): %s", vespalib::getErrorString(errno).c_str());
        } else {
            LOG(debug, "Successfully invoked prctl(PR_SET_NO_NEW_PRIVS)");
        }
    }
#endif
}

}