-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/consrv: add experimental privilege dropping on gokrazy
Implementation is more involved than NTP's simpler re-execute self and reuse file descriptor 3 technique. We need privileges to read config files, open serial port devices, and depending on the port choice of the user (if <1024) listen on sockets. Privileged Resource Acquisition Changes: - Move SSH and debug HTTP server listening socket creation outside of goroutines to ensure all the privileged operations are sequentially ordered before privdrop. - Modify `serveDebug` to receive a net.Listener and call `net.http.Server.Serve` instead. Command Line Flag: - Adds `-experimental-drop-privileges` boolean flag, defaulting to false. Privdrop, when enabled: - On Gokrazy: - Gated behind build constraint tag `gokrazy` - Creates an empty directory under /dev/shm and chroot()s to it. - Changes GID and UID to conventional nobody/nogroup ID 65534 - Other platforms: - Returns a "not implemented" error. Co-authored-by: Matt Layher <mdlayher@gmail.com> Signed-off-by: Matt Layher <mdlayher@gmail.com>
- Loading branch information
Showing
3 changed files
with
123 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2023 Berk D. Demir and Matt Layher | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build gokrazy | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
const ( | ||
privdropUID = 65534 // conventionally: nobody | ||
privdropGID = 65534 // conventionally: nogroup | ||
) | ||
|
||
func dropPrivileges() (*privilegesInfo, error) { | ||
dir, err := os.MkdirTemp("/dev/shm", "consrv-chroot-*") | ||
if err != nil { | ||
return nil, fmt.Errorf("create chroot directory: %w", err) | ||
} | ||
|
||
if err := syscall.Chroot(dir); err != nil { | ||
return nil, fmt.Errorf("chroot %q: %w", dir, err) | ||
} | ||
|
||
if err := syscall.Setgid(privdropGID); err != nil { | ||
return nil, fmt.Errorf("setgid: %w", err) | ||
} | ||
|
||
if err := syscall.Setuid(privdropUID); err != nil { | ||
return nil, fmt.Errorf("setuid: %w", err) | ||
} | ||
|
||
return &privilegesInfo{ | ||
Chroot: dir, | ||
UID: privdropUID, | ||
GID: privdropGID, | ||
}, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 Berk D. Demir and Matt Layher | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !gokrazy | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
func dropPrivileges() (*privilegesInfo, error) { | ||
return nil, fmt.Errorf("implemented only on gokrazy, not on %s/%s", runtime.GOOS, runtime.GOARCH) | ||
} |