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

If chcon fails, check if label is already correct #177

Merged
merged 1 commit into from
Sep 28, 2022

Conversation

rhatdan
Copy link
Collaborator

@rhatdan rhatdan commented Jun 30, 2022

Currently if a user attempts to chcon a file or directory and fails for
any reason check if the file already has the right label, and continue.

Signed-off-by: Daniel J Walsh [email protected]

@rhatdan
Copy link
Collaborator Author

rhatdan commented Jun 30, 2022

@giuseppe @thaJeztah @kolyshkin PTAL

@rhatdan
Copy link
Collaborator Author

rhatdan commented Jun 30, 2022

Attempt to help fix: containers/podman#14786

@rhatdan
Copy link
Collaborator Author

rhatdan commented Jun 30, 2022

jkroon81 PTAL

giuseppe
giuseppe previously approved these changes Jun 30, 2022
Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah
Copy link
Member

I think golangci-lint must be updated to a newer version to work with Go 1.18 (we can likely drop older go versions from the CI matrix as well)

@rhatdan
Copy link
Collaborator Author

rhatdan commented Jul 8, 2022

@thaJeztah How do I update lint?

@thaJeztah
Copy link
Member

@rhatdan whoops, saw your comment, but forgot to reply; I opened #179 to update

Comment on lines 18 to 20
if currentLabel, err := lFileLabel(fpath); err == nil {
if label == currentLabel {
slowMode = true
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Slightly wondering if it's easier to read if it's a one-liner;

Suggested change
if currentLabel, err := lFileLabel(fpath); err == nil {
if label == currentLabel {
slowMode = true
}
}
if currentLabel, err := lFileLabel(fpath); err == nil && label == currentLabel {
slowMode = true
}

go.mod Outdated

require golang.org/x/sys v0.0.0-20191115151921-52ab43148777
require golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to do this in a separate commit (keeping the "fix" separate from assisting changes)

@@ -12,7 +12,22 @@ import (
)

func rchcon(fpath, label string) error {
slowMode := false
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps a silly question, but what does "slowMode" mean here? Looking at the changes, it seems that slowMode actually enables a "fast path" (check the label, and we're done), but the name of this variable feels like it has the reverse meaning 😅

Or is reading the label (lFileLabel()) slower than setting it ( lSetFileLabel()) ?

Comment on lines 18 to 20
if currentLabel, err := lFileLabel(fpath); err == nil {
if label == currentLabel {
slowMode = true
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Slightly wondering if it'd be easier to read if this was a one-liner;

Suggested change
if currentLabel, err := lFileLabel(fpath); err == nil {
if label == currentLabel {
slowMode = true
}
}
if currentLabel, err := lFileLabel(fpath); err == nil && label == currentLabel {
slowMode = true
}

Comment on lines 1112 to 1095
flabel, _ := lFileLabel(fpath)
if flabel == label {
Copy link
Member

Choose a reason for hiding this comment

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

I guess it's ok(isn), but we don't check the error here, so technically can't assume flabel is an ok value

Comment on lines 1105 to 1098
err := lSetFileLabel(fpath, label)
if err == nil {
return nil
}
if errors.Is(err, os.ErrNotExist) {
return err
}
flabel, _ := lFileLabel(fpath)
if flabel == label {
return nil
}
return err
Copy link
Member

Choose a reason for hiding this comment

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

Somewhat related to my earlier question; if reading back the label is faster than setting it, should we reverse this?

Currently, this;

  • tries to set the label
  • if that fails for whatever reason, other than "file doesn't exist", we try to read the existing label
  • if that fails, we return the first error

If reading is indeed faster, we could do;

Suggested change
err := lSetFileLabel(fpath, label)
if err == nil {
return nil
}
if errors.Is(err, os.ErrNotExist) {
return err
}
flabel, _ := lFileLabel(fpath)
if flabel == label {
return nil
}
return err
if flabel, err := lFileLabel(fpath); err == nil && flabel == label {
// already has the right label
return nil
}
return lSetFileLabel(fpath, label)

Currently if a user attempts to chcon a file or directory and fails for
any reason check if the file already has the right label, and continue.

Signed-off-by: Daniel J Walsh <[email protected]>
@rhatdan
Copy link
Collaborator Author

rhatdan commented Sep 26, 2022

@thaJeztah PTAL again.

@rhatdan
Copy link
Collaborator Author

rhatdan commented Sep 26, 2022

@mrunalp @vrothberg @kolyshkin PTAL

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.

3 participants