cgroupfs: Handle invalid PID/PGID on migration.

Reported-by: syzbot+670d686c42a0a8d7f8a6@syzkaller.appspotmail.com
PiperOrigin-RevId: 437096386
This commit is contained in:
Rahat Mahmood
2022-03-24 15:09:56 -07:00
committed by gVisor bot
parent bbec9ce251
commit 5835bc8c3a
2 changed files with 21 additions and 1 deletions
+7 -1
View File
@@ -302,6 +302,9 @@ func (d *cgroupProcsData) Write(ctx context.Context, fd *vfs.FileDescription, sr
t := kernel.TaskFromContext(ctx)
currPidns := t.ThreadGroup().PIDNamespace()
targetTG := currPidns.ThreadGroupWithID(kernel.ThreadID(tgid))
if targetTG == nil {
return 0, linuxerr.EINVAL
}
return n, targetTG.MigrateCgroup(d.Cgroup(fd))
}
@@ -341,6 +344,9 @@ func (d *tasksData) Write(ctx context.Context, fd *vfs.FileDescription, src user
t := kernel.TaskFromContext(ctx)
currPidns := t.ThreadGroup().PIDNamespace()
targetTask := currPidns.TaskWithID(kernel.ThreadID(tid))
if targetTask == nil {
return 0, linuxerr.EINVAL
}
return n, targetTask.MigrateCgroup(d.Cgroup(fd))
}
@@ -362,7 +368,7 @@ func parseInt64FromString(ctx context.Context, src usermem.IOSequence) (val, len
if err != nil {
// Note: This also handles zero-len writes if offset is beyond the end
// of src, or src is empty.
ctx.Warningf("cgroupfs.parseInt64FromString: failed to parse %q: %v", str, err)
ctx.Debugf("cgroupfs.parseInt64FromString: failed to parse %q: %v", str, err)
return 0, int64(n), linuxerr.EINVAL
}
+14
View File
@@ -403,6 +403,20 @@ TEST(Cgroup, MigrateToSubcontainerThread) {
EXPECT_FALSE(tasks.contains(tid));
}
TEST(Cgroup, MigrateInvalidPID) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
EXPECT_THAT(c.WriteControlFile("cgroup.procs", "-1"), PosixErrorIs(EINVAL));
EXPECT_THAT(c.WriteControlFile("cgroup.procs", "not-a-number"),
PosixErrorIs(EINVAL));
EXPECT_THAT(c.WriteControlFile("tasks", "-1"), PosixErrorIs(EINVAL));
EXPECT_THAT(c.WriteControlFile("tasks", "not-a-number"),
PosixErrorIs(EINVAL));
}
// Regression test for b/222278194.
TEST(Cgroup, DuplicateUnlinkOnDirFD) {
SKIP_IF(!CgroupsAvailable());