-
Notifications
You must be signed in to change notification settings - Fork 814
Tap plugin - alternative PR #794
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
Closed
mmirecki
wants to merge
1
commit into
containernetworking:main
from
mmirecki:tap_alternativeSolution
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package security | ||
|
||
import ( | ||
"fmt" | ||
"github.com/opencontainers/selinux/go-selinux" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
type SecurityModule interface { | ||
PrepareSecurityModule() error | ||
RequiresReboot() bool | ||
ReRunCommandWithSecurityLabels(addLinkString string, tmpName string, mtu int, nsFd int, multique bool, mac string) error | ||
} | ||
|
||
type NoSecurityModel struct { | ||
} | ||
|
||
type SELinuxSecurityModel struct { | ||
} | ||
|
||
func GetSecurityModule() SecurityModule { | ||
if selinux.EnforceMode() != -1 { | ||
return SELinuxSecurityModel{} | ||
} | ||
return NoSecurityModel{} | ||
} | ||
|
||
func (sm NoSecurityModel) PrepareSecurityModule() error { | ||
return nil | ||
} | ||
func (sm NoSecurityModel) RequiresReboot() bool { | ||
return false | ||
} | ||
func (sm NoSecurityModel) ReRunCommandWithSecurityLabels(addLinkString string, tmpName string, mtu int, nsFd int, multique bool, mac string) error { | ||
return nil | ||
} | ||
|
||
func (sm SELinuxSecurityModel) PrepareSecurityModule() error { | ||
output, err := exec.Command("getsebool", "container_use_devices").CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("failed to run getsebool command %s: %v", string(output), err) | ||
} | ||
if strings.Contains(string(output), "off") { | ||
output, err := exec.Command("setsebool", "-P", "container_use_devices", "true").CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("failed to run setsebool command %s: %v", string(output), err) | ||
} | ||
} | ||
return nil | ||
} | ||
func (sm SELinuxSecurityModel) RequiresReboot() bool { | ||
return true | ||
} | ||
|
||
// This is a workaround for a SELinux issue when creating taps in containers. | ||
// The tap device must be created with approperiate SE Linux labels, which the plugin is missing. | ||
// To achieve this we run another copy of the plugin with the required labels applied. This will | ||
// create the tap device from a process with the correct labels. The controll then returns to the | ||
// main instance of the plugin which performs the rest of the configuration. | ||
func (sm SELinuxSecurityModel) ReRunCommandWithSecurityLabels(addLinkString string, tmpName string, mtu int, nsFd int, multique bool, mac string) error { | ||
// Apply the appropriate se linux label. This will affect the newly executed plugin process. | ||
if err := selinux.SetExecLabel("system_u:system_r:container_t:s0"); err != nil { | ||
return fmt.Errorf("failed set socket label: %v", err) | ||
} | ||
minFDToCloseOnExec := 3 | ||
maxFDToCloseOnExec := 256 | ||
// we want to share the parent process std{in|out|err} - fds 0 through 2. | ||
// Since the FDs are inherited on fork / exec, we close on exec all others. | ||
for fd := minFDToCloseOnExec; fd < maxFDToCloseOnExec; fd++ { | ||
syscall.CloseOnExec(fd) | ||
} | ||
|
||
args := []string{addLinkString, tmpName, strconv.Itoa(mtu), strconv.Itoa(nsFd), strconv.FormatBool(multique), mac} | ||
output, err := exec.Command(os.Args[0], args...).CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("failed to run a nested plugin call (%s %s) to add link: %s: %v", os.Args[0], strings.Join(args, ", "), output, err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see these labels are hard-coded; is that the norm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is specific to just the selinux security module. Other modules would of course have different mechanisms (if present).
This PR was a proposal on how to extract the security module specific items from the main plugin code, but it seems this approach has not met with approval either, so I'm closing it.