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

Renaming *Mappings fields and use int* for mountEntry.fd #3939

Merged
merged 3 commits into from
Jul 21, 2023

Conversation

eiffel-fl
Copy link
Contributor

Hi.

This PR is a follow-up to #3717.
It fixes #3936 and #3935.

If you see any way to improve it, feel free to share.

Best regards.

@eiffel-fl eiffel-fl force-pushed the francis/caps-naming branch 7 times, most recently from 9a7cce4 to 2e9093f Compare July 18, 2023 12:42
@eiffel-fl eiffel-fl changed the title Renaming Mappings field and use int for srcFD Renaming *Mappings fields and use int for srcFD Jul 18, 2023
Copy link
Member

@rata rata left a comment

Choose a reason for hiding this comment

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

This LGTM.

I added one comment of another simplification that can be done on top of this (don't amend this commit).

Curious what @thaJeztah thinks about this.

libcontainer/rootfs_linux.go Outdated Show resolved Hide resolved
@@ -13,10 +13,10 @@ var (
// different when user namespaces are enabled.
func (c Config) HostUID(containerId int) (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.UidMappings == nil {
if c.UIDMappings == nil {
Copy link
Member

Choose a reason for hiding this comment

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

maybe we should use len(c.UIDMappings) == 0 here?

Copy link
Member

Choose a reason for hiding this comment

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

@eiffel-fl the ==nil should be removed. If it is nil the len is always 0. If it is not nil, the length can be 0 too.

So no need to keep both checks, we can just check for the len and that is enough.

Copy link
Member

Choose a reason for hiding this comment

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

It seems like we didn't solve it but marked as resolved. Did I miss something here?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, maybe forgot at that time, thank you for your remind. This PR addressed too much problems in ONE PR(combine FD fields and rename *idMapping). Feel free to open an new PR to resolve this problem.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

The new PR is here: #3947

libcontainer/container_linux.go Outdated Show resolved Hide resolved
@eiffel-fl eiffel-fl force-pushed the francis/caps-naming branch 2 times, most recently from ade2907 to 5ef5f41 Compare July 20, 2023 10:07
@eiffel-fl eiffel-fl changed the title Renaming *Mappings fields and use int for srcFD Renaming *Mappings fields and use int* for mountEntry.fd Jul 20, 2023
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

overall, I like this!

I left some comments, but nothing critical afaics

@@ -13,10 +13,10 @@ var (
// different when user namespaces are enabled.
func (c Config) HostUID(containerId int) (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.UidMappings == nil {
if c.UIDMappings == nil || len(c.UIDMappings) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Very tiny nit: this could be simplified to just;

Suggested change
if c.UIDMappings == nil || len(c.UIDMappings) == 0 {
if len(c.UIDMappings) == 0 {

a nil slice has length 0; https://go.dev/play/p/tSRvH2e-yH0

package main

import "fmt"

func main() {
	var foo []int = nil
	if len(foo) == 0 {
		fmt.Println("YUP!")
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the clarification! I always forgot about this!

Copy link
Member

Choose a reason for hiding this comment

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

No worries! There was nothing really wrong with the old code, just not really needed, so where possible, I try to keep things simple 🤗

libcontainer/configs/config_linux.go Show resolved Hide resolved
@@ -40,13 +40,12 @@ type mountConfig struct {
// mountEntry contains mount data specific to a mount point.
type mountEntry struct {
*configs.Mount
srcFD string
idmapFD int
fd *int
Copy link
Member

Choose a reason for hiding this comment

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

I was curious if (now that we no longer use magic -1 values) we needed to use an unsigned-integer, but it looks like unix.MoveMount() expects an int; https://github.com/golang/sys/blob/c406141231ada89c399c39dd52dd1e279c509f5c/unix/zsyscall_linux.go#L1272-L1288

func MoveMount(fromDirfd int, fromPathName string, toDirfd int, toPathName string, flags int) (err error) {

That said, mountViaFDs does construct the /proc/self/fd/ path, so slightly wondering if we should have something unsigned.

Any thoughts @kolyshkin @fuweid @rata ?

Copy link
Member

Choose a reason for hiding this comment

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

@thaJeztah I'd use *int, fds are usually ints. I see we won't use the negative values now that we have a pointer to represent "don't use this", but if the functions we want to use take ints in any case, why would we want unsigned?

We might have bigger values stored in an unsiged int that we can represent with int (that is what several syscalls take). So, it seems like it can even create issues.

Am I missing something? I like int* :)

Copy link
Member

Choose a reason for hiding this comment

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

No, not missing something directly, just thought that /proc/self/fd/-123 would be invalid, and having something unsigned could save us having to deal with that 😂

Copy link
Member

Choose a reason for hiding this comment

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

/proc/self/fd/-123 would be invalid

Yeah. But it might be easy to debug I think 🤔

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@kolyshkin PTAL

libcontainer/rootfs_linux.go Outdated Show resolved Hide resolved
libcontainer/rootfs_linux.go Outdated Show resolved Hide resolved
libcontainer/rootfs_linux.go Outdated Show resolved Hide resolved
libcontainer/configs/validate/rootless_test.go Outdated Show resolved Hide resolved
libcontainer/rootfs_linux.go Outdated Show resolved Hide resolved
@eiffel-fl eiffel-fl force-pushed the francis/caps-naming branch 4 times, most recently from 2de0421 to c09eabb Compare July 21, 2023 11:38
libcontainer/configs/validate/rootless_test.go Outdated Show resolved Hide resolved
libcontainer/configs/validate/rootless_test.go Outdated Show resolved Hide resolved
libcontainer/configs/validate/rootless_test.go Outdated Show resolved Hide resolved
libcontainer/configs/validate/rootless_test.go Outdated Show resolved Hide resolved
Previously to this commit, we used a string for srcFD as /proc/self/fd/NN.
This commit modified to this behavior, so srcFD is only an *int and the full path
is constructed in mountViaFDs() if srcFD is different than nil.

Signed-off-by: Francis Laniel <[email protected]>
We cannot have both srcFD and idMapFD set at the same time.
So, we can simplify this struct to only have one field which is used a srcFD
most of the time and as idMapFD when we do an id map mount.

Signed-off-by: Francis Laniel <[email protected]>
Copy link
Member

@lifubang lifubang left a comment

Choose a reason for hiding this comment

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

LGTM
Thanks!

@eiffel-fl
Copy link
Contributor Author

LGTM Thanks!

Thank you and sorry for the misunderstanding 😅!

Copy link
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@kolyshkin kolyshkin merged commit 74895d4 into opencontainers:main Jul 21, 2023
36 checks passed
@eiffel-fl
Copy link
Contributor Author

Awesome! Thank you for the merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider renaming Config.UidMappings and GidMappings to UIDMappings and GIDMappings
6 participants