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

selinux.Chcon should check legal rather then just label.Relabel #181

Merged
merged 1 commit into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 0 additions & 46 deletions go-selinux/label/label_linux.go
Expand Up @@ -3,8 +3,6 @@ package label
import (
"errors"
"fmt"
"os"
"os/user"
"strings"

"github.com/opencontainers/selinux/go-selinux"
Expand Down Expand Up @@ -113,50 +111,6 @@ func Relabel(path string, fileLabel string, shared bool) error {
return nil
}

exclude_paths := map[string]bool{
"/": true,
"/bin": true,
"/boot": true,
"/dev": true,
"/etc": true,
"/etc/passwd": true,
"/etc/pki": true,
"/etc/shadow": true,
"/home": true,
"/lib": true,
"/lib64": true,
"/media": true,
"/opt": true,
"/proc": true,
"/root": true,
"/run": true,
"/sbin": true,
"/srv": true,
"/sys": true,
"/tmp": true,
"/usr": true,
"/var": true,
"/var/lib": true,
"/var/log": true,
}

if home := os.Getenv("HOME"); home != "" {
exclude_paths[home] = true
}

if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
if usr, err := user.Lookup(sudoUser); err == nil {
exclude_paths[usr.HomeDir] = true
}
}

if path != "/" {
path = strings.TrimSuffix(path, "/")
}
if exclude_paths[path] {
return fmt.Errorf("SELinux relabeling of %s is not allowed", path)
}

if shared {
c, err := selinux.NewContext(fileLabel)
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions go-selinux/selinux_linux.go
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"math/big"
"os"
"os/user"
"path"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -1083,6 +1084,50 @@ func chcon(fpath string, label string, recurse bool) error {
return nil
}

exclude_paths := map[string]bool{
"/": true,
"/bin": true,
"/boot": true,
"/dev": true,
"/etc": true,
"/etc/passwd": true,
"/etc/pki": true,
"/etc/shadow": true,
"/home": true,
"/lib": true,
"/lib64": true,
"/media": true,
"/opt": true,
"/proc": true,
"/root": true,
"/run": true,
"/sbin": true,
"/srv": true,
"/sys": true,
"/tmp": true,
"/usr": true,
"/var": true,
"/var/lib": true,
"/var/log": true,
}

if home := os.Getenv("HOME"); home != "" {
exclude_paths[home] = true
}

if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" {
if usr, err := user.Lookup(sudoUser); err == nil {
exclude_paths[usr.HomeDir] = true
}
}
Comment on lines +1114 to +1122
Copy link
Member

Choose a reason for hiding this comment

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

Will this part bring back the issue you ran into that you were trying to solve in #180?

But some users put homedirectories under /usr, and I see no reason to block them from relabeling.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No the #180 was blocking /usr/*
Now we are just blocking /usr or $HOME not $HOME/*

Copy link
Member

Choose a reason for hiding this comment

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

Ah, gotcha!


if fpath != "/" {
fpath = strings.TrimSuffix(fpath, "/")
}
if exclude_paths[fpath] {
return fmt.Errorf("SELinux relabeling of %s is not allowed", fpath)
}

if !recurse {
return setFileLabel(fpath, label)
}
Expand Down