Skip to content

Commit

Permalink
Fix seek on /proc/pid/cmdline when task is zombie.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 348056159
  • Loading branch information
nlacasse authored and gvisor-bot committed Dec 17, 2020
1 parent 028271b commit 1ea241e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pkg/sentry/fsimpl/proc/task_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func getMM(task *kernel.Task) *mm.MemoryManager {
// MemoryManager's users count is incremented, and must be decremented by the
// caller when it is no longer in use.
func getMMIncRef(task *kernel.Task) (*mm.MemoryManager, error) {
if task.ExitState() == kernel.TaskExitDead {
return nil, syserror.ESRCH
}
var m *mm.MemoryManager
task.WithMuLocked(func(t *kernel.Task) {
m = t.MemoryManager()
Expand Down Expand Up @@ -111,9 +108,13 @@ var _ dynamicInode = (*auxvData)(nil)

// Generate implements vfs.DynamicBytesSource.Generate.
func (d *auxvData) Generate(ctx context.Context, buf *bytes.Buffer) error {
if d.task.ExitState() == kernel.TaskExitDead {
return syserror.ESRCH
}
m, err := getMMIncRef(d.task)
if err != nil {
return err
// Return empty file.
return nil
}
defer m.DecUsers(ctx)

Expand Down Expand Up @@ -157,9 +158,13 @@ var _ dynamicInode = (*cmdlineData)(nil)

// Generate implements vfs.DynamicBytesSource.Generate.
func (d *cmdlineData) Generate(ctx context.Context, buf *bytes.Buffer) error {
if d.task.ExitState() == kernel.TaskExitDead {
return syserror.ESRCH
}
m, err := getMMIncRef(d.task)
if err != nil {
return err
// Return empty file.
return nil
}
defer m.DecUsers(ctx)

Expand Down Expand Up @@ -472,7 +477,7 @@ func (fd *memFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64
}
m, err := getMMIncRef(fd.inode.task)
if err != nil {
return 0, nil
return 0, err
}
defer m.DecUsers(ctx)
// Buffer the read data because of MM locks
Expand Down
27 changes: 27 additions & 0 deletions test/syscalls/linux/proc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,33 @@ TEST(ProcPidCmdline, SubprocessForkSameCmdline) {
}
}

TEST(ProcPidCmdline, SubprocessSeekCmdline) {
FileDescriptor fd;
ASSERT_NO_ERRNO(WithSubprocess(
[&](int pid) -> PosixError {
// Running. Open /proc/pid/cmdline.
ASSIGN_OR_RETURN_ERRNO(
fd, Open(absl::StrCat("/proc/", pid, "/cmdline"), O_RDONLY));
return NoError();
},
[&](int pid) -> PosixError {
// Zombie, but seek should still succeed.
int ret = lseek(fd.get(), 0x801, 0);
if (ret < 0) {
return PosixError(errno);
}
return NoError();
},
[&](int pid) -> PosixError {
// Exited.
int ret = lseek(fd.get(), 0x801, 0);
if (ret < 0) {
return PosixError(errno);
}
return NoError();
}));
}

// Test whether /proc/PID/ symlinks can be read for a running process.
TEST(ProcPidSymlink, SubprocessRunning) {
char buf[1];
Expand Down

0 comments on commit 1ea241e

Please sign in to comment.