Files
vHost/backend/internal/isolation/schedule_native_linux_test.go
T

14 lines
2.0 KiB
Go

//go:build linux
package isolation
import("context";"fmt";"os";"os/user";"path/filepath";"strconv";"strings";"testing";"time")
func TestScheduledIdentityAndForeignPIDProtection(t *testing.T){
identity:=os.Getenv("SEA_QA_HOST_IDENTITY");if identity==""{t.Skip("managed disposable identity not supplied")};if os.Geteuid()!=0||!managedWebHostPattern.MatchString(identity){t.Fatal("root test with a managed identity required")};account,e:=user.Lookup(identity);if e!=nil{t.Fatal(e)};uid,_:=strconv.Atoi(account.Uid);gid,_:=strconv.Atoi(account.Gid);if uid==0||gid==0{t.Fatal("refusing privileged identity")}
root,e:=os.MkdirTemp("/tmp","sea-vhost-schedule-native-");if e!=nil{t.Fatal(e)};defer os.RemoveAll(root);if e=os.Chown(root,uid,gid);e!=nil{t.Fatal(e)}
out,e:=runScheduledIdentity(context.Background(),identity,"/usr/bin/id",[]string{"-u"},root,nil);if e!=nil||strings.TrimSpace(out.Output)!=account.Uid{t.Fatal("scheduled task did not drop UID",e)}
marker:="$(touch "+filepath.Join(root,"injected")+")";out,e=runScheduledIdentity(context.Background(),identity,"/bin/sh",[]string{"-c",`printf '%s' "$QA_MARKER"`},root,map[string]string{"QA_MARKER":marker});if e!=nil||out.Output!=marker{t.Fatal("environment was not passed literally",e)};if _,e=os.Stat(filepath.Join(root,"injected"));!os.IsNotExist(e){t.Fatal("environment executed as shell")}
ctx,cancel:=context.WithTimeout(context.Background(),100*time.Millisecond);defer cancel();_,_=runScheduledIdentity(ctx,identity,"/bin/sh",[]string{"-c",`(sleep 1; echo escaped > escaped) & wait`},root,nil);time.Sleep(1300*time.Millisecond);if _,e=os.Stat(filepath.Join(root,"escaped"));!os.IsNotExist(e){t.Fatal("timed-out task left a normal descendant running")}
config:="/var/lib/sea-vhost-agent/php/"+identity+".conf";if e=signalPHPAsIdentity(context.Background(),config,"TERM",os.Getpid());e==nil{t.Fatal("unprivileged FPM identity signaled the root test runner")}
fmt.Println("isolated UID, literal environment, process-group timeout and foreign root PID protection passed")
}