-
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.
Signed-off-by: Matt Layher <mdlayher@gmail.com>
- Loading branch information
Showing
4 changed files
with
83 additions
and
73 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 was deleted.
Oops, something went wrong.
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,45 +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 ( | ||
"log" | ||
"fmt" | ||
"os" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
const ( | ||
PRIVDROP_UID = 65534 // conventionally: nobody | ||
PRIVDROP_GID = 65534 // conventionally: nogroup | ||
CHROOT_PARENT_DIR = "/dev/shm" | ||
CHROOT_DIR_PATTERN = "consrv-chroot-*" | ||
privdropUID = 65534 // conventionally: nobody | ||
privdropGID = 65534 // conventionally: nogroup | ||
) | ||
|
||
func dropPrivileges(cond *sync.Cond, ll *log.Logger) { | ||
ll.Printf("droping privileges") | ||
chrootDir, err := os.MkdirTemp(CHROOT_PARENT_DIR, CHROOT_DIR_PATTERN) | ||
func dropPrivileges() (*privilegesInfo, error) { | ||
dir, err := os.MkdirTemp("/dev/shm", "consrv-chroot-*") | ||
if err != nil { | ||
ll.Fatalf("couldn't create an empty directory under %s to chroot into: %v", CHROOT_PARENT_DIR, err) | ||
return nil, fmt.Errorf("create chroot directory: %w", err) | ||
} | ||
|
||
err = syscall.Chroot(chrootDir) | ||
if err != nil { | ||
ll.Fatalf("couldn't chroot to %s: %v", chrootDir, err) | ||
if err := syscall.Chroot(dir); err != nil { | ||
return nil, fmt.Errorf("chroot %q: %w", dir, err) | ||
} | ||
ll.Printf("chroot'ed into %s", chrootDir) | ||
|
||
err = syscall.Setgid(PRIVDROP_GID) | ||
if err != nil { | ||
ll.Fatalf("couldn't setgid to %d: %v", PRIVDROP_GID, err) | ||
if err := syscall.Setgid(privdropGID); err != nil { | ||
return nil, fmt.Errorf("setgid: %w", err) | ||
} | ||
|
||
err = syscall.Setuid(PRIVDROP_UID) | ||
if err != nil { | ||
ll.Fatalf("couldn't setuid to %d: %v", PRIVDROP_UID, err) | ||
if err := syscall.Setuid(privdropUID); err != nil { | ||
return nil, fmt.Errorf("setuid: %w", err) | ||
} | ||
|
||
ll.Printf("changed to uid=%d gid=%d", PRIVDROP_UID, PRIVDROP_GID) | ||
|
||
cond.Broadcast() | ||
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 |
---|---|---|
@@ -1,12 +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 ( | ||
"log" | ||
"sync" | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
func dropPrivileges(_ *sync.Cond, ll *log.Logger) { | ||
ll.Printf("Privdrop is not implemented for this platform yet") | ||
func dropPrivileges() (*privilegesInfo, error) { | ||
return nil, fmt.Errorf("implemented only on gokrazy, not on %s/%s", runtime.GOOS, runtime.GOARCH) | ||
} |