Project

General

Profile

Actions

Bug #14271

closed

libkstat leaks kstat fd across exec

Added by Robert Mustacchi 9 months ago. Updated 8 months ago.

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

100%

Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:

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 #1

Updated by Electric Monk 9 months ago

  • Gerrit CR set to 1830
Actions #2

Updated by Robert Mustacchi 8 months ago

To test this I reran the above test program with the fixed library and a pfiles of the resulting process no longer showed the kstat device.

Actions #3

Updated by Robert Mustacchi 8 months ago

  • Subject changed from libkstat leads kstat fd across exec to libkstat leaks kstat fd across exec
Actions #4

Updated by Electric Monk 8 months ago

  • Status changed from New to Closed
  • % Done changed from 0 to 100

git commit 92c32ccad68fb065a366d11e8f3dfcd5c6ebae94

commit  92c32ccad68fb065a366d11e8f3dfcd5c6ebae94
Author: Robert Mustacchi <rm@fingolfin.org>
Date:   2021-12-18T23:07:51.000Z

    14271 libkstat leaks kstat fd across exec
    Reviewed by: Jason King <jason.brian.king@gmail.com>
    Reviewed by: Patrick Mooney <pmooney@pfmooney.com>
    Reviewed by: Andy Fiddaman <andy@omnios.org>
    Approved by: Dan McDonald <danmcd@joyent.com>

Actions

Also available in: Atom PDF