pkg / term

Package term manages POSIX terminals.
BSD 2-Clause "Simplified" License
393 stars 64 forks source link

FD leak in open() #68

Closed stephan57160 closed 2 years ago

stephan57160 commented 2 years ago

The code of term.Open() does not close the FD, in case of error:

// Open opens an asynchronous communications port.
func Open(name string, options ...func(*Term) error) (*Term, error) {
    fd, e := unix.Open(name, unix.O_NOCTTY|unix.O_CLOEXEC|unix.O_NDELAY|unix.O_RDWR, 0666)
    if e != nil {
        // FD not opened, so, we don't care.
        return nil, &os.PathError{
            Op:   "open",
            Path: name,
            Err:  e,
        }
    }

    orig, err := termios.Tcgetattr(uintptr(fd))
    if err != nil {
        // FD opened, but not closed.
        return nil, err
    }
    t := Term{name: name, fd: fd, orig: *orig}
    if err := t.SetOption(options...); err != nil {
        // FD open, but not closed.
        return nil, err
    }

    // FD opened, but not closed if SetNonblock() fails.
    return &t, unix.SetNonblock(t.fd, false)
}

Leak may happen, when the supposed TERM file is not a TERM.

davecheney commented 2 years ago

good catch, would you like to send a PR?

stephan57160 commented 2 years ago

Not sure I can test it fully, but why not, yes.

give me 1h or so.

Le 15-Dec-21 à 11:45, Dave Cheney a écrit :

good catch, would you like to send a PR?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pkg/term/issues/68#issuecomment-994666632, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI7SRNO4FBZHUOPL5IA2KGTURBWWNANCNFSM5KDH7EZQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

stephan57160 commented 2 years ago

Well... Actually, I found almost the same in

stephan57160 commented 2 years ago

PR #69 raised, in case you have some time to check it.