-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: CNI: avoid error with iptables setuid check (release-4.2)
Pick #3444 Newer versions of the `iptables` command use a real vs effective uid check whether they are being called from a setuid script, and exit if that's the case. This check was added because iptables can call out to binaries / libraries on `PATH` / `LD_LIBRARY_PATH`, and these are generally under control of the user - allowing privilege escalation attackes. Singularity sanitizes the environment before running CNI plugins, which will call `iptables`, so we can set both real and effective uid to 0 to avoid the error. While we are here, make `PATH` sanitization the default in the network code, rather than relying on the caller applying it. Fixes #3318
- Loading branch information
Showing
5 changed files
with
35 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,30 +1,37 @@ | ||
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved. | ||
// Copyright (c) 2018-2024, Sylabs Inc. All rights reserved. | ||
// This software is licensed under a 3-clause BSD license. Please consult the | ||
// LICENSE.md file distributed with the sources of this project regarding your | ||
// rights to use or distribute this software. | ||
|
||
package priv | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"syscall" | ||
|
||
"github.com/sylabs/singularity/v4/pkg/sylog" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// Escalate escalates privileges of the thread or process. | ||
// Since Go 1.16 syscall.Setresuid is an all-thread operation. | ||
// A runtime.LockOSThread operation remains for older versions of Go. | ||
func Escalate() error { | ||
// EscalateRealEffective escalates real and effective uid of the thread or | ||
// process to root (0). The current real uid is set as the saved set-user-ID. | ||
// Since Go 1.16 syscall.Setresuid is an all-thread operation. A | ||
// runtime.LockOSThread operation remains for older versions of Go. | ||
func EscalateRealEffective() error { | ||
runtime.LockOSThread() | ||
uid := os.Getuid() | ||
return syscall.Setresuid(uid, 0, uid) | ||
uid, _, _ := unix.Getresuid() | ||
sylog.Debugf("Escalate r/e/s: %d/%d/%d", 0, 0, uid) | ||
return syscall.Setresuid(0, 0, uid) | ||
} | ||
|
||
// Drop drops privileges of the thread or process. | ||
// Since Go 1.16 syscall.Setresuid is an all-thread operation. | ||
// A runtime.LockOSThread operation remains for older versions of Go. | ||
// Drop drops privileges of the thread or process. The real and effective uid | ||
// are set to the value of the saved set-user-ID. The saved set-user-ID is | ||
// returned to 0 to allow escalation in future. Since Go 1.16 syscall.Setresuid | ||
// is an all-thread operation. A runtime.LockOSThread operation remains for | ||
// older versions of Go. | ||
func Drop() error { | ||
defer runtime.UnlockOSThread() | ||
uid := os.Getuid() | ||
_, _, uid := unix.Getresuid() | ||
sylog.Debugf("Drop r/e/s: %d/%d/%d", uid, uid, 0) | ||
return syscall.Setresuid(uid, uid, 0) | ||
} |
This file contains 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