@@ -4076,4 +4076,114 @@ extension IntegrationSuite {
40764076 throw error
40774077 }
40784078 }
4079+
4080+ func testNoNewPrivileges( ) async throws {
4081+ let id = " test-no-new-privileges "
4082+
4083+ let bs = try await bootstrap ( id)
4084+ let buffer = BufferWriter ( )
4085+ let container = try LinuxContainer ( id, rootfs: bs. rootfs, vmm: bs. vmm) { config in
4086+ config. process. arguments = [ " cat " , " /proc/self/status " ]
4087+ config. process. noNewPrivileges = true
4088+ config. process. stdout = buffer
4089+ config. bootLog = bs. bootLog
4090+ }
4091+
4092+ try await container. create ( )
4093+ try await container. start ( )
4094+
4095+ let status = try await container. wait ( )
4096+ try await container. stop ( )
4097+
4098+ guard status. exitCode == 0 else {
4099+ throw IntegrationError . assert ( msg: " process status \( status) != 0 " )
4100+ }
4101+
4102+ guard let output = String ( data: buffer. data, encoding: . utf8) else {
4103+ throw IntegrationError . assert ( msg: " failed to convert stdout to UTF8 " )
4104+ }
4105+
4106+ // /proc/self/status contains "NoNewPrivs:\t1" when the bit is set
4107+ guard output. contains ( " NoNewPrivs: \t 1 " ) else {
4108+ throw IntegrationError . assert ( msg: " expected NoNewPrivs to be 1, got: \( output) " )
4109+ }
4110+ }
4111+
4112+ func testNoNewPrivilegesDisabled( ) async throws {
4113+ let id = " test-no-new-privileges-disabled "
4114+
4115+ let bs = try await bootstrap ( id)
4116+ let buffer = BufferWriter ( )
4117+ let container = try LinuxContainer ( id, rootfs: bs. rootfs, vmm: bs. vmm) { config in
4118+ config. process. arguments = [ " cat " , " /proc/self/status " ]
4119+ // noNewPrivileges defaults to false
4120+ config. process. stdout = buffer
4121+ config. bootLog = bs. bootLog
4122+ }
4123+
4124+ try await container. create ( )
4125+ try await container. start ( )
4126+
4127+ let status = try await container. wait ( )
4128+ try await container. stop ( )
4129+
4130+ guard status. exitCode == 0 else {
4131+ throw IntegrationError . assert ( msg: " process status \( status) != 0 " )
4132+ }
4133+
4134+ guard let output = String ( data: buffer. data, encoding: . utf8) else {
4135+ throw IntegrationError . assert ( msg: " failed to convert stdout to UTF8 " )
4136+ }
4137+
4138+ // When noNewPrivileges is not set, NoNewPrivs should be 0
4139+ guard output. contains ( " NoNewPrivs: \t 0 " ) else {
4140+ throw IntegrationError . assert ( msg: " expected NoNewPrivs to be 0, got: \( output) " )
4141+ }
4142+ }
4143+
4144+ func testNoNewPrivilegesExec( ) async throws {
4145+ let id = " test-no-new-privileges-exec "
4146+
4147+ let bs = try await bootstrap ( id)
4148+ let container = try LinuxContainer ( id, rootfs: bs. rootfs, vmm: bs. vmm) { config in
4149+ config. process. arguments = [ " sleep " , " 100 " ]
4150+ config. bootLog = bs. bootLog
4151+ }
4152+
4153+ do {
4154+ try await container. create ( )
4155+ try await container. start ( )
4156+
4157+ // Exec a process with noNewPrivileges set
4158+ let buffer = BufferWriter ( )
4159+ let exec = try await container. exec ( " nnp-exec " ) { config in
4160+ config. arguments = [ " cat " , " /proc/self/status " ]
4161+ config. noNewPrivileges = true
4162+ config. stdout = buffer
4163+ }
4164+
4165+ try await exec. start ( )
4166+ let status = try await exec. wait ( )
4167+ try await exec. delete ( )
4168+
4169+ guard status. exitCode == 0 else {
4170+ throw IntegrationError . assert ( msg: " exec status \( status) != 0 " )
4171+ }
4172+
4173+ guard let output = String ( data: buffer. data, encoding: . utf8) else {
4174+ throw IntegrationError . assert ( msg: " failed to convert stdout to UTF8 " )
4175+ }
4176+
4177+ guard output. contains ( " NoNewPrivs: \t 1 " ) else {
4178+ throw IntegrationError . assert ( msg: " expected NoNewPrivs to be 1 in exec, got: \( output) " )
4179+ }
4180+
4181+ try await container. kill ( SIGKILL)
4182+ try await container. wait ( )
4183+ try await container. stop ( )
4184+ } catch {
4185+ try ? await container. stop ( )
4186+ throw error
4187+ }
4188+ }
40794189}
0 commit comments