Skip to content

Commit d61d842

Browse files
committed
Disable mounting cgroups by default (miklos.szegedi@cloudera.com via rkanter)
(cherry picked from commit 351cf87)
1 parent 34afb9f commit d61d842

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[] = {"yarn", "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
*/
@@ -2198,20 +2205,25 @@ void chown_dir_contents(const char *dir_path, uid_t uid, gid_t gid) {
21982205
DIR *dp;
21992206
struct dirent *ep;
22002207

2201-
char *path_tmp = malloc(strlen(dir_path) + NAME_MAX + 2);
2208+
size_t len = strlen(dir_path) + NAME_MAX + 2;
2209+
char *path_tmp = malloc(len);
22022210
if (path_tmp == NULL) {
22032211
return;
22042212
}
22052213

2206-
char *buf = stpncpy(path_tmp, dir_path, strlen(dir_path));
2207-
*buf++ = '/';
2208-
22092214
dp = opendir(dir_path);
22102215
if (dp != NULL) {
22112216
while ((ep = readdir(dp)) != NULL) {
2212-
stpncpy(buf, ep->d_name, strlen(ep->d_name));
2213-
buf[strlen(ep->d_name)] = '\0';
2214-
change_owner(path_tmp, uid, gid);
2217+
if (strcmp(ep->d_name, ".") != 0 &&
2218+
strcmp(ep->d_name, "..") != 0 &&
2219+
strstr(ep->d_name, "..") == NULL) {
2220+
int result = snprintf(path_tmp, len, "%s/%s", dir_path, ep->d_name);
2221+
if (result > 0 && result < len) {
2222+
change_owner(path_tmp, uid, gid);
2223+
} else {
2224+
fprintf(LOGFILE, "Ignored %s/%s due to length", dir_path, ep->d_name);
2225+
}
2226+
}
22152227
}
22162228
closedir(dp);
22172229
}
@@ -2235,25 +2247,27 @@ int mount_cgroup(const char *pair, const char *hierarchy) {
22352247
char *mount_path = malloc(len);
22362248
char hier_path[EXECUTOR_PATH_MAX];
22372249
int result = 0;
2238-
struct stat sb;
22392250

22402251
if (controller == NULL || mount_path == NULL) {
22412252
fprintf(LOGFILE, "Failed to mount cgroup controller; not enough memory\n");
22422253
result = OUT_OF_MEMORY;
2254+
goto cleanup;
2255+
}
2256+
if (hierarchy == NULL || strstr(hierarchy, "..") != NULL) {
2257+
fprintf(LOGFILE, "Unsupported cgroup hierarhy path detected.\n");
2258+
result = INVALID_COMMAND_PROVIDED;
2259+
goto cleanup;
22432260
}
22442261
if (get_kv_key(pair, controller, len) < 0 ||
22452262
get_kv_value(pair, mount_path, len) < 0) {
22462263
fprintf(LOGFILE, "Failed to mount cgroup controller; invalid option: %s\n",
22472264
pair);
22482265
result = -1;
22492266
} else {
2250-
if (stat(mount_path, &sb) != 0) {
2251-
// Create mount point, if it does not exist
2252-
const mode_t mount_perms = S_IRWXU | S_IRGRP | S_IXGRP;
2253-
if (mkdirs(mount_path, mount_perms) == 0) {
2254-
fprintf(LOGFILE, "Failed to create cgroup mount point %s at %s\n",
2255-
controller, mount_path);
2256-
}
2267+
if (strstr(mount_path, "..") != NULL) {
2268+
fprintf(LOGFILE, "Unsupported cgroup mount path detected.\n");
2269+
result = INVALID_COMMAND_PROVIDED;
2270+
goto cleanup;
22572271
}
22582272
if (mount("none", mount_path, "cgroup", 0, controller) == 0) {
22592273
char *buf = stpncpy(hier_path, mount_path, strlen(mount_path));
@@ -2262,20 +2276,28 @@ int mount_cgroup(const char *pair, const char *hierarchy) {
22622276

22632277
// create hierarchy as 0750 and chown to Hadoop NM user
22642278
const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
2279+
struct stat sb;
2280+
if (stat(hier_path, &sb) == 0 &&
2281+
(sb.st_uid != nm_uid || sb.st_gid != nm_gid)) {
2282+
fprintf(LOGFILE, "cgroup hierarchy %s already owned by another user %d\n", hier_path, sb.st_uid);
2283+
result = INVALID_COMMAND_PROVIDED;
2284+
goto cleanup;
2285+
}
22652286
if (mkdirs(hier_path, perms) == 0) {
22662287
change_owner(hier_path, nm_uid, nm_gid);
22672288
chown_dir_contents(hier_path, nm_uid, nm_gid);
22682289
}
22692290
} else {
22702291
fprintf(LOGFILE, "Failed to mount cgroup controller %s at %s - %s\n",
2271-
controller, mount_path, strerror(errno));
2292+
controller, mount_path, strerror(errno));
22722293
// if controller is already mounted, don't stop trying to mount others
22732294
if (errno != EBUSY) {
22742295
result = -1;
22752296
}
22762297
}
22772298
}
22782299

2300+
cleanup:
22792301
free(controller);
22802302
free(mount_path);
22812303

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
extern struct passwd *user_detail;
@@ -235,6 +236,9 @@ int is_feature_enabled(const char* feature_key, int default_value,
235236
/** Check if tc (traffic control) support is enabled in configuration. */
236237
int is_tc_support_enabled();
237238

239+
/** Check if cgroup mount support is enabled in configuration. */
240+
int is_mount_cgroups_support_enabled();
241+
238242
/**
239243
* Run a batch of tc commands that modify interface configuration
240244
*/

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
@@ -257,14 +257,19 @@ static int validate_arguments(int argc, char **argv , int *operation) {
257257
}
258258

259259
if (strcmp("--mount-cgroups", argv[1]) == 0) {
260-
if (argc < 4) {
261-
display_usage(stdout);
262-
return INVALID_ARGUMENT_NUMBER;
260+
if (is_mount_cgroups_support_enabled()) {
261+
if (argc < 4) {
262+
display_usage(stdout);
263+
return INVALID_ARGUMENT_NUMBER;
264+
}
265+
optind++;
266+
cmd_input.cgroups_hierarchy = argv[optind++];
267+
*operation = MOUNT_CGROUPS;
268+
return 0;
269+
} else {
270+
display_feature_disabled_message("mount cgroup");
271+
return FEATURE_DISABLED;
263272
}
264-
optind++;
265-
cmd_input.cgroups_hierarchy = argv[optind++];
266-
*operation = MOUNT_CGROUPS;
267-
return 0;
268273
}
269274

270275
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)