Project

General

Profile

Actions

Bug #14271

closed

libkstat leaks kstat fd across exec

Added by Robert Mustacchi 10 months ago. Updated 10 months ago.

Status:
Closed
Priority:
Normal
Category:
lib - userland libraries
Start date:
Due date:
% Done:

100%

Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:
External Bug:

Description

While working through #14264 I was updating the manual pages and saw a note about not needing clean up after an exec. I looked at the source code and didn't see anything there at all related to that. To test this, I wrote up a simple program:

#include <stdio.h>
#include <kstat.h>
#include <err.h>
#include <unistd.h>
#include <stdlib.h>

int
main(void)
{
        kstat_ctl_t *kc = kstat_open();
        if (kc == NULL) {
                err(EXIT_FAILURE, "failed to open libkstat");
        }

        execl("/usr/bin/sleep", "sleep", "100000", NULL);
        return (0);
}

So I executed this, I then ran pfiles on this and saw that lo and behold, we still had the FD:

8303:   sleep 100000
  Current rlimit: 65536 file descriptors
   0: S_IFCHR mode:0620 dev:540,0 ino:592550986 uid:1000 gid:7 rdev:169,2
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /dev/pts/2
      offset:3276346
   1: S_IFCHR mode:0620 dev:540,0 ino:592550986 uid:1000 gid:7 rdev:169,2
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /dev/pts/2
      offset:3276346
   2: S_IFCHR mode:0620 dev:540,0 ino:592550986 uid:1000 gid:7 rdev:169,2
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /dev/pts/2
      offset:3276346
   3: S_IFCHR mode:0666 dev:539,0 ino:62390276 uid:0 gid:3 rdev:119,0
      O_RDONLY|O_LARGEFILE
      /devices/pseudo/kstat@0:kstat
      offset:0

The issue here is a missing O_CLOEXEC when we open the kstat handle.

Actions

Also available in: Atom PDF