Skip to content

Commit 3b5925e

Browse files
isaac-fletcherfacebook-github-bot
authored andcommitted
Implement Windows superuser check (facebookincubator#568)
Summary: Previously, TTPForge would only log a warning when a TTP required superuser privileges on Windows but wouldn't actually enforce the requirement. This change implements proper administrator privilege checking on Windows using `golang.org/x/sys/windows` to check if the process token is elevated. The implementation uses Go build tags to provide platform-specific implementations of superuser checking. Unix-based systems continue to use `os.Geteuid() == 0` while Windows now uses token elevation checking. Reviewed By: RoboticPrism Differential Revision: D85156306
1 parent a139cc3 commit 3b5925e

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

example-ttps/requirements/os-and-superuser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ steps:
2020
- name: demo
2121
print_str: |
2222
If you see this string, you are executing this TTP
23-
with superuser privileges within a compabile OS/Architecture environment.
23+
with superuser privileges within a compatible OS/Architecture environment.

pkg/blocks/requirements.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ THE SOFTWARE.
2020
package blocks
2121

2222
import (
23-
"errors"
2423
"fmt"
25-
"os"
2624
"runtime"
2725

2826
"github.com/facebookincubator/ttpforge/pkg/checks"
@@ -93,13 +91,12 @@ func (rc *RequirementsConfig) Verify(ctx checks.VerificationContext) error {
9391

9492
// check superuser requirement
9593
if rc.ExpectSuperuser {
94+
if !isSuperuser() {
95+
return fmt.Errorf("must be running with elevated privileges to run this TTP")
96+
}
9697
if runtime.GOOS == "windows" {
97-
logging.L().Warnf("not enforcing superuser requirement because it is not supported on windows yet")
98+
logging.L().Debug("[+] Running as administrator")
9899
} else {
99-
if os.Geteuid() != 0 {
100-
err := errors.New("must be root (UID 0) to run this TTP")
101-
return err
102-
}
103100
logging.L().Debug("[+] Running as root")
104101
}
105102
}

pkg/blocks/requirements_unix.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build unix
2+
3+
/*
4+
Copyright © 2023-present, Meta Platforms, Inc. and affiliates
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
*/
21+
22+
package blocks
23+
24+
import "os"
25+
26+
// isSuperuser checks if the current process is running with superuser privileges
27+
// on Unix-like systems (Linux, macOS, BSD, etc.) by checking if the effective user ID is 0 (root).
28+
func isSuperuser() bool {
29+
return os.Geteuid() == 0
30+
}

pkg/blocks/requirements_windows.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build windows
2+
3+
/*
4+
Copyright © 2023-present, Meta Platforms, Inc. and affiliates
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
*/
21+
22+
package blocks
23+
24+
import "golang.org/x/sys/windows"
25+
26+
// isSuperuser checks if the current process is running with administrator privileges
27+
// on Windows by checking if the process token is elevated.
28+
func isSuperuser() bool {
29+
token := windows.GetCurrentProcessToken()
30+
return token.IsElevated()
31+
}

0 commit comments

Comments
 (0)