Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idmap improvements #1509

Merged
merged 4 commits into from Apr 16, 2017
Merged

idmap improvements #1509

merged 4 commits into from Apr 16, 2017

Conversation

brauner
Copy link
Member

@brauner brauner commented Apr 15, 2017

Signed-off-by: Christian Brauner christian.brauner@ubuntu.com

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch 2 times, most recently from e78ea55 to 208a0da Compare April 15, 2017 13:13
@brauner brauner changed the title conf: lxc_map_ids() non-functional changes idmap improvements Apr 15, 2017
@brauner brauner requested a review from hallyn April 15, 2017 14:09
src/lxc/conf.c Outdated
if (!has_sufficient_privilege &&
lxc_file_cap_is_set(newuidmap_path, CAP_SETUID, CAP_EFFECTIVE) &&
lxc_file_cap_is_set(newuidmap_path, CAP_SETUID, CAP_PERMITTED) &&
sb.st_uid == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File does not need to be owned by root for filecaps to take effect. The sb.st_uid check can be removed afaics.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would still be able to write to new_{g,u}idmap even if euid > 0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write to it? Aren't you going to execute it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean will new{g,u}idmap be able to write to /proc/<pid>/uid_map when it is not uid 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless of course, uid_map is owned by another the unpriv user.

src/lxc/conf.c Outdated
free(cmdpath);
newuidmap_path = on_path("newuidmap", NULL);
if (newuidmap_path) {
bool has_sufficient_privilege = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block sure seems like a good candidate for a helper function :) Not requisite for this patch, just a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thought about it as well. Might just write it now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you turn that block into a helper then it goes away? :)

In either case, the check here to break out of the loop needs to be for fret right? or not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, that last comment was supposed to go with the block below.

src/lxc/conf.c Outdated
} else {
left = LXC_IDMAPLEN - (pos - buf);
fill = snprintf(pos, left, "\n");
if (fill <= 0 || fill >= left)
SYSERROR("Too many {g,u}id mappings defined.");
pos += fill;
ret = system(buf);
fret = system(buf);
}
if (ret)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this check still be on ret, or on fret? What was the reason for switching from ret to fret?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ret is used in the new{g,u}idmap block before and might get initialized to a non-zero value but the for-loop, as unlikely as this is, might not be run and then you'd exit with error.

@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch from 208a0da to 238ab1f Compare April 15, 2017 16:19
src/lxc/conf.c Outdated
return -1;
}

if (has_privileged_newuidmap && !has_privileged_newgidmap) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to error out when we find only one of the two binaries.

src/lxc/utils.c Outdated
/* Check if it has file capabilities. */
if (lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) &&
lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED) &&
st.st_uid == uid) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this in for now. But if we're sure I'll remove it before the merge.

src/lxc/conf.c Outdated
if (cmdpath) {
use_shadow = 1;
free(cmdpath);
newuidmap_path = on_path("newuidmap", NULL);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't move the on_path() part into the setuid checks. But I probably could. Should we do that, what do you think?

@hallyn
Copy link
Member

hallyn commented Apr 15, 2017 via email

@hallyn
Copy link
Member

hallyn commented Apr 15, 2017 via email

@hallyn
Copy link
Member

hallyn commented Apr 15, 2017 via email

@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch 3 times, most recently from f5f350c to 608d9d0 Compare April 15, 2017 19:07
@brauner brauner changed the title idmap improvements [WIP] idmap improvements Apr 15, 2017
@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch from 608d9d0 to ac5f143 Compare April 15, 2017 20:34
@brauner brauner changed the title [WIP] idmap improvements idmap improvements Apr 15, 2017
@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch from ac5f143 to 22f5365 Compare April 15, 2017 20:49
Add two new helpers that allow to determine whether a given proc or file has a
capability in the given set and move lxc_cap_is_set() to static function that
both call internally.

Closes lxc#296.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch from 22f5365 to 032768a Compare April 15, 2017 20:50
@brauner
Copy link
Member Author

brauner commented Apr 15, 2017

@hallyn, updated.

@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch from 032768a to 2c45c2c Compare April 15, 2017 21:24
@hallyn
Copy link
Member

hallyn commented Apr 15, 2017

Hi,

looks good. Just two more comments for now:

  1. unfortunately (if i read this right) writing to map_file also requires cap_sys_admin against the user_ns, in addition to cap_setuid/cap_setgid.
  2. newgidmap is actually setuid-root, not setgid-root. So the filecaps check should check for CAP_SETGID and CAP_SYS_ADMIN, but the 'check if the binary is setuid' check should check for setuid-root.

Christian Brauner added 2 commits April 16, 2017 00:59
The new{g,u}idmap binaries where a source of trouble for users when they lacked
sufficient privileges. This commit adds code to check for sufficient privilege.
It checks whether new{g,u}idmap is root owned and has the setuid bit set and if
it doesn't it checks whether new{g,u}idmap is root owned and has CAP_SETUID in
its CAP_PERMITTED and CAP_EFFECTIVE set.

Closes lxc#296.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
@brauner brauner force-pushed the 2017-04-15/improve_lxc_id_map branch from 2c45c2c to 91c3e28 Compare April 15, 2017 23:00
@brauner
Copy link
Member Author

brauner commented Apr 15, 2017

Hi,

looks good. Just two more comments for now:

  1. unfortunately (if i read this right) writing to map_file also requires cap_sys_admin against the user_ns, in addition to cap_setuid/cap_setgid.
  2. newgidmap is actually setuid-root, not setgid-root. So the filecaps check should check for CAP_SETGID and CAP_SYS_ADMIN, but the 'check if the binary is setuid' check should check for setuid-root.

As discussed on IRC CAP_SYS_ADMIN is actually not required. What we need to check is:

    1. newuidmap has the setuid bit set --> good to go
    1. newuidmap has CAP_SETUID file capability in CAP_EFFECTIVE and CAP_PERMITTED --> good to go
    1. newgidmap has the setuid bit set --> good to go
    1. newgidmap has CAP_SETGID file capability in CAP_EFFECTIVE and CAP_PERMITTED --> good to go.
      Anything else we don't consider for now.

@brauner
Copy link
Member Author

brauner commented Apr 15, 2017

@hallyn, should be ready to merge.

@hallyn
Copy link
Member

hallyn commented Apr 16, 2017

Awesome, thanks.

@hallyn hallyn merged commit 93caf97 into lxc:master Apr 16, 2017
evgeni added a commit to evgeni/lxc that referenced this pull request Apr 17, 2017
Signed-off-by: Evgeni Golov <evgeni@debian.org>
brauner pushed a commit that referenced this pull request Apr 17, 2017
stgraber pushed a commit that referenced this pull request Apr 18, 2017
Signed-off-by: Evgeni Golov <evgeni@debian.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants