Don't panic on user-controlled state in semaphore syscalls.

Reported-by: syzbot+beb099a67f670386a367@syzkaller.appspotmail.com
PiperOrigin-RevId: 386521361
This commit is contained in:
Rahat Mahmood
2021-07-23 13:37:33 -07:00
committed by gVisor bot
parent 0eea96057a
commit 3d0a930005
2 changed files with 16 additions and 5 deletions
+5 -5
View File
@@ -214,15 +214,14 @@ func (r *Registry) Remove(id ipc.ID, creds *auth.Credentials) error {
r.mu.Lock()
defer r.mu.Unlock()
r.reg.Remove(id, creds)
index, found := r.findIndexByID(id)
if !found {
// Inconsistent state.
panic(fmt.Sprintf("unable to find an index for ID: %d", id))
return linuxerr.EINVAL
}
delete(r.indexes, index)
r.reg.Remove(id, creds)
return nil
}
@@ -245,7 +244,8 @@ func (r *Registry) newSetLocked(ctx context.Context, key ipc.Key, creator fs.Fil
index, found := r.findFirstAvailableIndex()
if !found {
panic("unable to find an available index")
// See linux, ipc/sem.c:newary().
return nil, linuxerr.ENOSPC
}
r.indexes[index] = set.obj.ID
+11
View File
@@ -1019,6 +1019,17 @@ TEST(SemaphoreTest, SemInfo) {
EXPECT_EQ(info.semvmx, kSemVmx);
}
TEST(SempahoreTest, RemoveNonExistentSemaphore) {
EXPECT_THAT(semctl(-1, 0, IPC_RMID), SyscallFailsWithErrno(EINVAL));
}
TEST(SempahoreTest, RemoveDeletedSemaphore) {
int id;
EXPECT_THAT(id = semget(IPC_PRIVATE, 1, 0), SyscallSucceeds());
EXPECT_THAT(semctl(id, 0, IPC_RMID), SyscallSucceeds());
EXPECT_THAT(semctl(id, 0, IPC_RMID), SyscallFailsWithErrno(EINVAL));
}
} // namespace
} // namespace testing
} // namespace gvisor