Skip to content

Fix parsing of user/group during copy operation#34143

Merged
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:fix-copy-permissions
Jan 20, 2025
Merged

Fix parsing of user/group during copy operation#34143
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:fix-copy-permissions

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

@thaJeztah thaJeztah commented Jul 17, 2017

relates to

If a container was started with

  • a numeric uid
  • both a user and group (username:groupname)
  • uid and gid (uid:gid)

The copy action failed, because the "username:groupname"
was looked up using getent.

This patch;

  • splits user and group before looking up
  • if numeric uid or gid is used and lookup fails,
    the uid / gid is used as-is

Comment thread daemon/archive_tarcopyoptions_unix.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/etc/passwd -> getent(1)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ah, yes, perhaps I should change that; I started writing for this PR, but found

moby/daemon/daemon_unix.go

Lines 941 to 951 in c8a2596

// Parse the remapped root (user namespace) option, which can be one of:
// username - valid username from /etc/passwd
// username:groupname - valid username; valid groupname from /etc/group
// uid - 32-bit unsigned int valid Linux UID value
// uid:gid - uid value; 32-bit unsigned int Linux GID value
//
// If no groupname is specified, and a username is specified, an attempt
// will be made to lookup a gid for that username as a groupname
//
// If names are used, they are verified to exist in passwd/group
func parseRemappedRoot(usergrp string) (string, string, error) {
, so copied parts from that

I'm not sure what approach should be taken though if the user needs to come from within the container (where getent may not be available, such as in a busybox container) - that's why I kept it as "WIP"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

updated 👍

@aaronlehmann
Copy link
Copy Markdown

@AkihiroSuda, does this look good now?

@AkihiroSuda
Copy link
Copy Markdown
Member

getent - > typically getent? (Sorry for going back and forth)

@AkihiroSuda
Copy link
Copy Markdown
Member

Is this still WIP?

@thaJeztah thaJeztah changed the title [WIP] Fix parsing of user/group during copy operation Fix parsing of user/group during copy operation Aug 1, 2017
@thaJeztah
Copy link
Copy Markdown
Member Author

let me remove "WIP", code itself should be generally ok (not sure how to test this, without the test becoming dependent on the host)

I think I made this WIP because it only resolves the parsing issue, and not the fact that this looks up the user/group from the host (#34142).

getent - > typically getent? (Sorry for going back and forth)

Oh, can you be more specific; happy to update but wasn't sure what you meant / where you wanted to have that updated 😄

Comment thread daemon/archive.go Outdated
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

oh, need to remove this one as that's part of #34099, so don't merge as-is

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

removed

@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from 1996def to e4a5d45 Compare August 1, 2017 08:26
@runcom
Copy link
Copy Markdown
Member

runcom commented Sep 18, 2017

any update on this?

@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from e4a5d45 to cbc2bbc Compare June 12, 2018 01:05
@olljanat
Copy link
Copy Markdown
Contributor

@thaJeztah this one needs to be rebased

@derek derek Bot added the status/failing-ci Indicates that the PR in its current state fails the test suite label Dec 22, 2018
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from cbc2bbc to 1aa8d22 Compare December 24, 2018 16:21
@thaJeztah thaJeztah added rebuild/z and removed status/failing-ci Indicates that the PR in its current state fails the test suite labels Dec 24, 2018
@thaJeztah
Copy link
Copy Markdown
Member Author

Rebased @tonistiigi @AkihiroSuda PTAL

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 8, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@54dddad). Click here to learn what that means.
The diff coverage is 0%.

@@            Coverage Diff            @@
##             master   #34143   +/-   ##
=========================================
  Coverage          ?   36.52%           
=========================================
  Files             ?      608           
  Lines             ?    45060           
  Branches          ?        0           
=========================================
  Hits              ?    16459           
  Misses            ?    26321           
  Partials          ?     2280

Comment thread daemon/archive_tarcopyoptions_unix.go Fixed
Comment thread daemon/archive_tarcopyoptions_unix.go Fixed
Comment thread daemon/archive_tarcopyoptions_unix.go Fixed
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch 2 times, most recently from 2c69e04 to bab4e16 Compare January 4, 2025 01:08
Comment thread daemon/archive_tarcopyoptions_unix.go Fixed
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from bab4e16 to 5bd409e Compare January 4, 2025 01:15
Comment thread daemon/archive_tarcopyoptions_unix.go Outdated

usr, err := lookupUser(ctr, userNameOrID)
if err != nil {
return 0, 0, errors.Wrapf(err, "failed to look up user %q in container: %w", userNameOrID)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, forgot to remove the %w when I swapped to errors.Wrapf;

daemon/archive_tarcopyoptions_unix.go:64:16: github.com/docker/docker/vendor/github.com/pkg/errors.Wrapf format %w reads arg #2, but call has 1 arg
daemon/archive_tarcopyoptions_unix.go:73:16: github.com/docker/docker/vendor/github.com/pkg/errors.Wrapf format %w reads arg #2, but call has 1 arg

@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from 5bd409e to 29b709c Compare January 6, 2025 08:29
Comment on lines +82 to +85
if uid, err := strconv.ParseUint(nameOrID, 10, 32); err == nil && uid <= math.MaxInt32 {
// uid provided
return int(uid), ""
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wondering if this is too strict, and if we should just use something like

filter := func(entry SubID) bool {
return entry.Name == u.Name || entry.Name == strconv.Itoa(u.Uid)
}

Comment thread daemon/archive_tarcopyoptions_unix.go Outdated
Comment on lines +57 to +67
if userName == "" && hasGroup && groupName == "" {
// UID/GID passed; nothing to look up.
return userID, groupID, nil
}

usr, err := lookupUser(ctr, userNameOrID)
if err != nil {
return 0, 0, errors.Wrapf(err, "failed to look up user %q in container", userNameOrID)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That said.. with the logic in this PR, we'd be able to skip lookup if a numeric UID/GID is passed; that means we're also able to work with a container that doesn't have /etc/passwd / etc/group.

@thompson-shaun thompson-shaun added this to the 29.0.0 milestone Jan 16, 2025
Comment thread daemon/archive_tarcopyoptions_unix.go Outdated
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from 29b709c to 63675e9 Compare January 16, 2025 20:36
@thaJeztah

This comment was marked as resolved.

@thaJeztah

This comment was marked as resolved.

@thaJeztah thaJeztah modified the milestones: 29.0.0, 28.0.0 Jan 17, 2025
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from 63675e9 to 046c40e Compare January 17, 2025 18:26
@thaJeztah thaJeztah assigned thaJeztah and unassigned aaronlehmann Jan 17, 2025
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch 4 times, most recently from 1eff567 to ee025b2 Compare January 18, 2025 00:34
If a container was started with

- a numeric uid
- both a user and group (username:groupname)
- uid and gid (uid:gid)

The copy action failed, because the "username:groupname"
was looked up using getent.

This patch;

- splits `user` and `group` before looking up
- if numeric `uid` or `gid` is used and lookup fails,
  the `uid` / `gid` is used as-is

The code also looked up the user and group on the host
instead of in the container when using getent; this patch
fixes the lookup to only use the container's /etc/passwd
and /etc/group instead.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the fix-copy-permissions branch from ee025b2 to f658ea3 Compare January 20, 2025 11:45
@thaJeztah thaJeztah dismissed cpuguy83’s stale review January 20, 2025 17:10

Integration tests were added

@thaJeztah
Copy link
Copy Markdown
Member Author

Let me bring this one in; thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet