Systemd/src/udev
David Herrmann ab7854df73 udev: don't close FDs before dropping them from epoll
Make sure we never close fds before we drop their related event-source.
This will cause horrible disruptions if the fd-num is re-used by someone
else. Under normal conditions, this should not cause any problems as the
close() will drop the fd from the epoll-set automatically. However, this
changes if you have any child processes with a copy of that fd.

This fixes issue #163.

Background:
        If you create an epoll-set via epoll_create() (lets call it 'EFD')
        you can add file-descriptors to it to watch for events. Whenever
        you call EPOLL_CTL_ADD on a file-descriptor you want to watch, the
        kernel looks up the attached "struct file" pointer, that this FD
        refers to. This combination of the FD-number and the "struct file"
        pointer is used as key to link it into the epoll-set (EFD).

        This means, if you duplicate your file-descriptor, you can watch
        this file-descriptor, too (because the duplicate will have a
        different FD-number, hence, the combination of FD-number and
        "struct file" is different as before).

        If you want to stop watching an FD, you use EPOLL_CTL_DEL and pass
        the FD to the kernel. The kernel again looks up your
        file-descriptor in your FD-table to find the linked "struct file".
        This FD-number and "struct file" combination is then dropped from
        the epoll-set (EFD).

        Last, but not least: If you close a file-descriptor that is linked
        to an epoll-set, the kernel does *NOTHING* regarding the
        epoll-set. This is a vital observation! Because this means, your
        epoll_wait() calls will still return the metadata you used to
        watch/subscribe your file-descriptor to events.
        There is one exception to this rule: If the file-descriptor that
        you just close()ed was the last FD that referred to the underlying
        "struct file", then _all_ epoll-set watches/subscriptions are
        destroyed. Hence, if you never dup()ed your FD, then a simple
        close() will also unsubscribe it from any epoll-set.

        With this in mind, lets look at fork():
                Assume you have an epoll-set (EFD) and a bunch of FDs
                subscribed to events on that EFD. If you now call fork(),
                the new process gets a copy of your file-descriptor table.
                This means, the whole table is copied and the "struct
                file" reference of each FD is increased by 1. It is
                important to notice that the FD-numbers in the child are
                exactly the same as in the parent (eg., FD #5 in the child
                refers to the same "struct file" as FD #5 in the parent).

                This means, if the child calls EPOLL_CTL_DEL on an FD, the
                kernel will look up the linked "struct file" and drop the
                FD-number and "struct file" combination from the epoll-set
                (EFD). However, this will effectively drop the
                subscription that was installed by the parent.

                To sum up: even though the child gets a duplicate of the
                EFD and all FDs, the subscriptions in the EFD are *NOT*
                duplicated!

Now, with this in mind, lets look at what udevd does:
        Udevd has a bunch of file-descriptors that it watches in its
        sd-event main-loop. Whenever a uevent is received, the event is
        dispatched on its workers. If no suitable worker is present, a new
        worker is fork()ed to handle the event. Inside of this worker, we
        try to free all resources we inherited. However, the fork() call
        is done from a call-stack that is never rewinded. Therefore, this
        call stack might own references that it drops once it is left.
        Those references we cannot deduce from the fork()'ed process;
        effectively causing us to leak objects in the worker (eg., the
        call to sd_event_dispatch() that dispatched our uevent owns a
        reference to the sd_event object it used; and drops it again once
        the function is left).

        (Another example is udev_monitor_ref() for each 'worker' that is
         also inherited by all children; thus keeping the udev-monitor and
         the uevent-fd alive in all children (which is the real cause for
         bug #163))

        (The extreme variant is sd_event_source_unref(), which explicitly
         keeps event-sources alive, if they're currently dispatched,
         knowing that the dispatcher will free the event once done. But
         if the dispatcher is in the parent, the child will never ever
         free that object, thus leaking it)

        This is usually not an issue. However, if such an object has a
        file-descriptor embedded, this FD is left open and never closed in
        the child.

In manager_exit(), if we now destroy an object (i.e., close its embedded
file-descriptor) before we destroy its related sd_event_source, then
sd-event will not be able to drop the FD from the epoll-set (EFD). This
is, because the FD is no longer valid at the time we call EPOLL_CTL_DEL.
Hence, the kernel cannot figure out the linked "struct file" and thus
cannot remove the FD-number plus "struct file" combination; effectively
leaving the subscription in the epoll-set.
Since we leak the uevent-fd in the children, they retain a copy of the FD
pointing to the same "struct file". Thus, the EFD-subscription are not
automatically removed by close() (as described above). Therefore, the main
daemon will still get its metadata back on epoll_watch() whenever an event
occurs (even though it already freed the metadata). This then causes the
free-after-use bug described in #163.

This patch fixes the order in which we destruct objects and related
sd-event-sources. Some open questions remain:

 * Why does source_io_unregister() not warn on EPOLL_CTL_DEL failures?
   This really needs to be turned into an assert_return().

 * udevd really should not leak file-descriptors into its children. Fixing
   this would *not* have prevented this bug, though (since the child-setup
   is still async).
   It's non-trivial to fix this, though. The stack-context of the caller
   cannot be rewinded, so we cannot figure out temporary refs. Maybe it's
   time to exec() the udev-workers?

 * Why does the kernel not copy FD-subscriptions across fork()?
   Or at least drop subscriptions if you close() your FD (it uses the
   FD-number as key, so it better subscribe to it)?
   Or it better used
         FD+"struct file_table*"+"struct file*"
   as key to not allow the childen to share the subscription table..
   *sigh*
   Seems like we have to live with that API forever.
2015-06-17 00:31:57 +02:00
..
accelerometer remove unused includes 2015-02-23 23:53:42 +01:00
ata_id ata_id: drop spurious space 2015-05-18 21:52:26 +02:00
cdrom_id shared: add random-util.[ch] 2015-04-11 00:11:13 +02:00
collect remove unused includes 2015-02-23 23:53:42 +01:00
mtd_probe build-sys: add one more Makefile symlink 2015-03-09 18:02:23 +01:00
net sd-netlink: rename from sd-rtnl 2015-06-13 19:52:54 +02:00
scsi_id shared: add random-util.[ch] 2015-04-11 00:11:13 +02:00
v4l_id v4l_id: use standard option parsing loop 2015-03-07 13:54:32 -05:00
.gitignore build-sys: generate CLEANFILES from EXTRA_DIST 2015-03-04 22:47:19 -05:00
.vimrc import udev repository 2012-04-03 21:08:04 +02:00
Makefile build-sys: add stub makefiles to all subdirs to ease development with emacs 2012-04-13 21:37:59 +02:00
udev-builtin-blkid.c udev: downgrade a few warnings to debug messages 2015-04-14 15:53:33 +02:00
udev-builtin-btrfs.c remove unused includes 2015-02-23 23:53:42 +01:00
udev-builtin-hwdb.c udev: add some asserts 2015-06-02 18:12:47 +02:00
udev-builtin-input_id.c udev: input_id - use ABS_MT_SLOT{-1} to exclude non touch screen devices 2015-06-03 08:31:02 +10:00
udev-builtin-keyboard.c udev: add some asserts 2015-06-02 18:12:47 +02:00
udev-builtin-kmod.c remove unused includes 2015-02-23 23:53:42 +01:00
udev-builtin-net_id.c udev: add some asserts 2015-06-02 18:12:47 +02:00
udev-builtin-net_setup_link.c udevadm,..: make --help output of udev tools more like the output of the various other tools 2015-01-05 13:19:55 +01:00
udev-builtin-path_id.c udev-builtin: path_id - don't pass NULL to udev_device_get_parent() 2015-06-02 18:12:47 +02:00
udev-builtin-uaccess.c remove unused includes 2015-02-23 23:53:42 +01:00
udev-builtin-usb_id.c udev: add some asserts 2015-06-02 18:12:47 +02:00
udev-builtin.c libudev: add missing hunks 2015-03-09 23:36:27 +01:00
udev-ctrl.c udev-ctrl: make _unref() always return NULL 2015-05-15 23:36:35 +02:00
udev-event.c Merge pull request #144 from teg/udev-spawn-log-less-2 2015-06-14 20:19:54 +02:00
udev-node.c shared: add formats-util.h 2015-04-10 23:54:48 +02:00
udev-rules.c udevd: event - don't log about failures of spawn processes when this is expected 2015-06-10 17:55:53 +02:00
udev-watch.c remove unused includes 2015-02-23 23:53:42 +01:00
udev.conf man: add udev.conf(5) 2014-07-31 08:56:03 -04:00
udev.h Merge pull request #144 from teg/udev-spawn-log-less-2 2015-06-14 20:19:54 +02:00
udev.pc.in udev.pc: install udev files to /lib/udev rather than /lib/systemd 2012-05-27 15:24:20 +02:00
udevadm-control.c remove unused includes 2015-02-23 23:53:42 +01:00
udevadm-hwdb.c Revert "hwdb: actually search /run/udev/hwdb.d" 2015-06-09 11:26:06 +02:00
udevadm-info.c udevadm: enclose invocation of unlinkat() with a (void) cast 2015-04-21 17:43:55 +02:00
udevadm-monitor.c shared: add formats-util.h 2015-04-10 23:54:48 +02:00
udevadm-settle.c udev: Fix ping timeout when settle timeout is 0 2015-04-22 19:16:58 +02:00
udevadm-test-builtin.c remove unused includes 2015-02-23 23:53:42 +01:00
udevadm-test.c udevd: simplify signal mask handling 2015-06-03 01:41:34 +02:00
udevadm-trigger.c remove unused includes 2015-02-23 23:53:42 +01:00
udevadm-util.c util: rework strappenda(), and rename it strjoina() 2015-02-03 02:05:59 +01:00
udevadm-util.h #pragma once here and there 2015-01-23 09:30:44 -05:00
udevadm.c remove unused includes 2015-02-23 23:53:42 +01:00
udevd.c udev: don't close FDs before dropping them from epoll 2015-06-17 00:31:57 +02:00