Skip to content

Commit f5fd5aa

Browse files
rkantermiklosszegedi
authored andcommitted
Disable mounting cgroups by default ([email protected] via rkanter)
(cherry picked from commit 351cf87) (cherry picked from commit d61d842)
1 parent 8a68a15 commit f5fd5aa

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static const char* DEFAULT_BANNED_USERS[] = {"mapred", "hdfs", "bin", 0};
7070

7171
static const int DEFAULT_DOCKER_SUPPORT_ENABLED = 0;
7272
static const int DEFAULT_TC_SUPPORT_ENABLED = 0;
73+
static const int DEFAULT_MOUNT_CGROUP_SUPPORT_ENABLED = 0;
7374

7475
//location of traffic control binary
7576
static const char* TC_BIN = "/sbin/tc";
@@ -469,6 +470,12 @@ int is_tc_support_enabled() {
469470
DEFAULT_TC_SUPPORT_ENABLED, &executor_cfg);
470471
}
471472

473+
int is_mount_cgroups_support_enabled() {
474+
return is_feature_enabled(MOUNT_CGROUP_SUPPORT_ENABLED_KEY,
475+
DEFAULT_MOUNT_CGROUP_SUPPORT_ENABLED,
476+
&executor_cfg);
477+
}
478+
472479
/**
473480
* Utility function to concatenate argB to argA using the concat_pattern.
474481
*/
@@ -2188,20 +2195,25 @@ void chown_dir_contents(const char *dir_path, uid_t uid, gid_t gid) {
21882195
DIR *dp;
21892196
struct dirent *ep;
21902197

2191-
char *path_tmp = malloc(strlen(dir_path) + NAME_MAX + 2);
2198+
size_t len = strlen(dir_path) + NAME_MAX + 2;
2199+
char *path_tmp = malloc(len);
21922200
if (path_tmp == NULL) {
21932201
return;
21942202
}
21952203

2196-
char *buf = stpncpy(path_tmp, dir_path, strlen(dir_path));
2197-
*buf++ = '/';
2198-
21992204
dp = opendir(dir_path);
22002205
if (dp != NULL) {
22012206
while ((ep = readdir(dp)) != NULL) {
2202-
stpncpy(buf, ep->d_name, strlen(ep->d_name));
2203-
buf[strlen(ep->d_name)] = '\0';
2204-
change_owner(path_tmp, uid, gid);
2207+
if (strcmp(ep->d_name, ".") != 0 &&
2208+
strcmp(ep->d_name, "..") != 0 &&
2209+
strstr(ep->d_name, "..") == NULL) {
2210+
int result = snprintf(path_tmp, len, "%s/%s", dir_path, ep->d_name);
2211+
if (result > 0 && result < len) {
2212+
change_owner(path_tmp, uid, gid);
2213+
} else {
2214+
fprintf(LOGFILE, "Ignored %s/%s due to length", dir_path, ep->d_name);
2215+
}
2216+
}
22052217
}
22062218
closedir(dp);
22072219
}
@@ -2225,25 +2237,27 @@ int mount_cgroup(const char *pair, const char *hierarchy) {
22252237
char *mount_path = malloc(len);
22262238
char hier_path[EXECUTOR_PATH_MAX];
22272239
int result = 0;
2228-
struct stat sb;
22292240

22302241
if (controller == NULL || mount_path == NULL) {
22312242
fprintf(LOGFILE, "Failed to mount cgroup controller; not enough memory\n");
22322243
result = OUT_OF_MEMORY;
2244+
goto cleanup;
2245+
}
2246+
if (hierarchy == NULL || strstr(hierarchy, "..") != NULL) {
2247+
fprintf(LOGFILE, "Unsupported cgroup hierarhy path detected.\n");
2248+
result = INVALID_COMMAND_PROVIDED;
2249+
goto cleanup;
22332250
}
22342251
if (get_kv_key(pair, controller, len) < 0 ||
22352252
get_kv_value(pair, mount_path, len) < 0) {
22362253
fprintf(LOGFILE, "Failed to mount cgroup controller; invalid option: %s\n",
22372254
pair);
22382255
result = -1;
22392256
} else {
2240-
if (stat(mount_path, &sb) != 0) {
2241-
// Create mount point, if it does not exist
2242-
const mode_t mount_perms = S_IRWXU | S_IRGRP | S_IXGRP;
2243-
if (mkdirs(mount_path, mount_perms) == 0) {
2244-
fprintf(LOGFILE, "Failed to create cgroup mount point %s at %s\n",
2245-
controller, mount_path);
2246-
}
2257+
if (strstr(mount_path, "..") != NULL) {
2258+
fprintf(LOGFILE, "Unsupported cgroup mount path detected.\n");
2259+
result = INVALID_COMMAND_PROVIDED;
2260+
goto cleanup;
22472261
}
22482262
if (mount("none", mount_path, "cgroup", 0, controller) == 0) {
22492263
char *buf = stpncpy(hier_path, mount_path, strlen(mount_path));
@@ -2252,20 +2266,28 @@ int mount_cgroup(const char *pair, const char *hierarchy) {
22522266

22532267
// create hierarchy as 0750 and chown to Hadoop NM user
22542268
const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
2269+
struct stat sb;
2270+
if (stat(hier_path, &sb) == 0 &&
2271+
(sb.st_uid != nm_uid || sb.st_gid != nm_gid)) {
2272+
fprintf(LOGFILE, "cgroup hierarchy %s already owned by another user %d\n", hier_path, sb.st_uid);
2273+
result = INVALID_COMMAND_PROVIDED;
2274+
goto cleanup;
2275+
}
22552276
if (mkdirs(hier_path, perms) == 0) {
22562277
change_owner(hier_path, nm_uid, nm_gid);
22572278
chown_dir_contents(hier_path, nm_uid, nm_gid);
22582279
}
22592280
} else {
22602281
fprintf(LOGFILE, "Failed to mount cgroup controller %s at %s - %s\n",
2261-
controller, mount_path, strerror(errno));
2282+
controller, mount_path, strerror(errno));
22622283
// if controller is already mounted, don't stop trying to mount others
22632284
if (errno != EBUSY) {
22642285
result = -1;
22652286
}
22662287
}
22672288
}
22682289

2290+
cleanup:
22692291
free(controller);
22702292
free(mount_path);
22712293

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ enum operations {
6161
#define ALLOWED_SYSTEM_USERS_KEY "allowed.system.users"
6262
#define DOCKER_SUPPORT_ENABLED_KEY "feature.docker.enabled"
6363
#define TC_SUPPORT_ENABLED_KEY "feature.tc.enabled"
64+
#define MOUNT_CGROUP_SUPPORT_ENABLED_KEY "feature.mount-cgroup.enabled"
6465
#define TMP_DIR "tmp"
6566

6667
/* Macros for min/max. */
@@ -243,6 +244,9 @@ int is_feature_enabled(const char* feature_key, int default_value,
243244
/** Check if tc (traffic control) support is enabled in configuration. */
244245
int is_tc_support_enabled();
245246

247+
/** Check if cgroup mount support is enabled in configuration. */
248+
int is_mount_cgroups_support_enabled();
249+
246250
/**
247251
* Run a batch of tc commands that modify interface configuration
248252
*/

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/main.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,19 @@ static int validate_arguments(int argc, char **argv , int *operation) {
247247
}
248248

249249
if (strcmp("--mount-cgroups", argv[1]) == 0) {
250-
if (argc < 4) {
251-
display_usage(stdout);
252-
return INVALID_ARGUMENT_NUMBER;
250+
if (is_mount_cgroups_support_enabled()) {
251+
if (argc < 4) {
252+
display_usage(stdout);
253+
return INVALID_ARGUMENT_NUMBER;
254+
}
255+
optind++;
256+
cmd_input.cgroups_hierarchy = argv[optind++];
257+
*operation = MOUNT_CGROUPS;
258+
return 0;
259+
} else {
260+
display_feature_disabled_message("mount cgroup");
261+
return FEATURE_DISABLED;
253262
}
254-
optind++;
255-
cmd_input.cgroups_hierarchy = argv[optind++];
256-
*operation = MOUNT_CGROUPS;
257-
return 0;
258263
}
259264

260265
if (strcmp("--tc-modify-state", argv[1]) == 0) {

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/NodeManagerCgroups.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ YARN uses CGroups through a directory structure mounted into the file system by
5050
| Option | Description |
5151
|:---- |:---- |
5252
| Discover CGroups mounted already | This should be used on newer systems like RHEL7 or Ubuntu16 or if the administrator mounts CGroups before YARN starts. Set `yarn.nodemanager.linux-container-executor.cgroups.mount` to false and leave other settings set to their defaults. YARN will locate the mount points in `/proc/mounts`. Common locations include `/sys/fs/cgroup` and `/cgroup`. The default location can vary depending on the Linux distribution in use.|
53-
| CGroups mounted by YARN | If the system does not have CGroups mounted or it is mounted to an inaccessible location then point `yarn.nodemanager.linux-container-executor.cgroups.mount-path` to an empty directory. Set `yarn.nodemanager.linux-container-executor.cgroups.mount` to true. A point to note here is that the container-executor binary will try to create and mount each subsystem as a subdirectory under this path. If `cpu` is already mounted somewhere with `cpuacct`, then the directory `cpu,cpuacct` will be created for the hierarchy.|
53+
| CGroups mounted by YARN | IMPORTANT: This option is deprecated due to security reasons with the `container-executor.cfg` option `feature.mount-cgroup.enabled=0` by default. Please mount cgroups before launching YARN.|
5454
| CGroups mounted already or linked but not in `/proc/mounts` | If cgroups is accessible through lxcfs or simulated by another filesystem, then point `yarn.nodemanager.linux-container-executor.cgroups.mount-path` to your CGroups root directory. Set `yarn.nodemanager.linux-container-executor.cgroups.mount` to false. YARN tries to use this path first, before any CGroup mount point discovery. The path should have a subdirectory for each CGroup hierarchy named by the comma separated CGroup subsystems supported like `<path>/cpu,cpuacct`. Valid subsystem names are `cpu, cpuacct, cpuset, memory, net_cls, blkio, freezer, devices`.|
5555

5656
CGroups and security

0 commit comments

Comments
 (0)