mirror of
https://github.com/MengMengCode/CLICD.git
synced 2026-12-05 12:59:20 +08:00
fix(security): validate transfer ownership IDs
This commit is contained in:
@@ -1929,10 +1929,11 @@ func parseSubIDRange(path, user string) (int, error) {
|
||||
if len(fields) != 3 || fields[0] != user {
|
||||
continue
|
||||
}
|
||||
start, startErr := strconv.Atoi(fields[1])
|
||||
count, countErr := strconv.Atoi(fields[2])
|
||||
if startErr == nil && countErr == nil && count >= 65536 {
|
||||
return start, nil
|
||||
start, startErr := strconv.ParseUint(fields[1], 10, 32)
|
||||
count, countErr := strconv.ParseUint(fields[2], 10, 32)
|
||||
maxInt := uint64(^uint(0) >> 1)
|
||||
if startErr == nil && countErr == nil && count >= 65536 && start <= maxInt && start+count <= uint64(1)<<32 {
|
||||
return int(start), nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("%s must contain a %s subordinate id range with at least 65536 ids", path, user)
|
||||
|
||||
@@ -5,12 +5,39 @@ package lxc
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const maxTransferOwnershipID = uint64(^uint32(0))
|
||||
|
||||
func checkedTransferOwnershipID(value int) (uint32, error) {
|
||||
if value < 0 || uint64(value) > maxTransferOwnershipID {
|
||||
return 0, fmt.Errorf("ownership ID %d is outside the uint32 range", value)
|
||||
}
|
||||
return uint32(value), nil
|
||||
}
|
||||
|
||||
func remapTransferOwnershipID(id, oldBase, newBase uint32) (uint32, bool, error) {
|
||||
var offset uint64
|
||||
switch {
|
||||
case oldBase > 0 && id >= oldBase && uint64(id-oldBase) < 65536:
|
||||
offset = uint64(id - oldBase)
|
||||
case id < 65536:
|
||||
offset = uint64(id)
|
||||
default:
|
||||
return id, false, nil
|
||||
}
|
||||
mapped := uint64(newBase) + offset
|
||||
if mapped > maxTransferOwnershipID {
|
||||
return 0, false, fmt.Errorf("remapped ownership ID exceeds uint32 range")
|
||||
}
|
||||
return uint32(mapped), true, nil
|
||||
}
|
||||
|
||||
func transferFileID(info os.FileInfo) ([2]uint64, bool) {
|
||||
st, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
@@ -22,6 +49,14 @@ func transferFileID(info os.FileInfo) ([2]uint64, bool) {
|
||||
// chown clears set-ID bits and file capabilities even when cloning an ext4
|
||||
// image. Preserve them while translating the subordinate-UID root ID.
|
||||
func remapTransferOwnership(path string, st os.FileInfo, uid, gid int, oldUID, oldGID uint32, newUID, newGID int) error {
|
||||
newUID32, err := checkedTransferOwnershipID(newUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newGID32, err := checkedTransferOwnershipID(newGID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
xattrs := map[string][]byte{}
|
||||
for _, name := range []string{"security.capability", "system.posix_acl_access", "system.posix_acl_default"} {
|
||||
size, err := unix.Lgetxattr(path, name, nil)
|
||||
@@ -37,19 +72,33 @@ func remapTransferOwnership(path string, st os.FileInfo, uid, gid int, oldUID, o
|
||||
}
|
||||
if name == "security.capability" && len(value) == 24 && binary.LittleEndian.Uint32(value[:4])&0xff000000 == 0x03000000 {
|
||||
root := binary.LittleEndian.Uint32(value[20:])
|
||||
if oldUID > 0 && root >= oldUID && root < oldUID+65536 {
|
||||
binary.LittleEndian.PutUint32(value[20:], root-oldUID+uint32(newUID))
|
||||
if mapped, changed, err := remapTransferOwnershipID(root, oldUID, newUID32); err != nil {
|
||||
return err
|
||||
} else if changed {
|
||||
binary.LittleEndian.PutUint32(value[20:], mapped)
|
||||
}
|
||||
}
|
||||
if name != "security.capability" && len(value) >= 4 {
|
||||
for offset := 4; offset+8 <= len(value); offset += 8 {
|
||||
tag := binary.LittleEndian.Uint16(value[offset:])
|
||||
id := binary.LittleEndian.Uint32(value[offset+4:])
|
||||
if tag == 0x02 && oldUID > 0 && id >= oldUID && id < oldUID+65536 {
|
||||
binary.LittleEndian.PutUint32(value[offset+4:], id-oldUID+uint32(newUID))
|
||||
if tag == 0x02 {
|
||||
mapped, changed, err := remapTransferOwnershipID(id, oldUID, newUID32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changed {
|
||||
binary.LittleEndian.PutUint32(value[offset+4:], mapped)
|
||||
}
|
||||
}
|
||||
if tag == 0x08 && oldGID > 0 && id >= oldGID && id < oldGID+65536 {
|
||||
binary.LittleEndian.PutUint32(value[offset+4:], id-oldGID+uint32(newGID))
|
||||
if tag == 0x08 {
|
||||
mapped, changed, err := remapTransferOwnershipID(id, oldGID, newGID32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if changed {
|
||||
binary.LittleEndian.PutUint32(value[offset+4:], mapped)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//go:build linux
|
||||
|
||||
package lxc
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTransferOwnershipIDConversionRejectsInvalidValues(t *testing.T) {
|
||||
if _, err := checkedTransferOwnershipID(-1); err == nil {
|
||||
t.Fatal("negative ownership ID was accepted")
|
||||
}
|
||||
if got, err := checkedTransferOwnershipID(100000); err != nil || got != 100000 {
|
||||
t.Fatalf("valid ownership ID conversion = %d, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransferOwnershipIDRemapRejectsUint32Overflow(t *testing.T) {
|
||||
if _, _, err := remapTransferOwnershipID(102, 100, ^uint32(0)-1); err == nil {
|
||||
t.Fatal("ownership ID remap overflow was accepted")
|
||||
}
|
||||
if got, changed, err := remapTransferOwnershipID(101, 100, ^uint32(0)-1); err != nil || !changed || got != ^uint32(0) {
|
||||
t.Fatalf("boundary ownership ID remap = %d, %t, %v", got, changed, err)
|
||||
}
|
||||
if got, changed, err := remapTransferOwnershipID(42, 0, 100000); err != nil || !changed || got != 100042 {
|
||||
t.Fatalf("unmapped guest ownership ID remap = %d, %t, %v", got, changed, err)
|
||||
}
|
||||
}
|
||||
@@ -51,3 +51,21 @@ func TestOwnershipRewriteHonorsCancellationAndPreservesUnchangedSetID(t *testing
|
||||
t.Fatal("ownership pass ignored cancellation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSubIDRangeRejectsOutOfRangeStarts(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "subuid")
|
||||
for _, value := range []string{"-1:65536", "4294967296:65536", "4294901761:65536"} {
|
||||
if err := os.WriteFile(path, []byte("root:"+value+"\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := parseSubIDRange(path, "root"); err == nil {
|
||||
t.Fatalf("accepted invalid subordinate ID range %q", value)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("root:100000:65536\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := parseSubIDRange(path, "root"); err != nil || got != 100000 {
|
||||
t.Fatalf("valid subordinate ID range = %d, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user