portal/sysfs: fix NULL-deref creating a dir under a net-namespaced kobject#94
Open
lacraig2 wants to merge 1 commit into
Open
portal/sysfs: fix NULL-deref creating a dir under a net-namespaced kobject#94lacraig2 wants to merge 1 commit into
lacraig2 wants to merge 1 commit into
Conversation
…bject
handle_op_sysfs_create_or_lookup_dir() created directories with
kobject_create_and_add(), whose kobject uses dynamic_kobj_ktype with a NULL
.namespace op. When the parent directory is network-namespaced -- anything under
/sys/class/net/<if> or /sys/devices/virtual/net/<if>, where the parent ktype's
child_ns_type reports KOBJ_NS_TYPE_NET -- the kernel's create_dir() ->
kobject_namespace() path calls kobj->ktype->namespace(kobj) unconditionally. With
a NULL op this is a NULL-deref Oops; it runs in the context of whatever task
triggered the portal op and was observed landing on PID 1 and killing the boot.
Non-net parents (e.g. /sys/bus/usb) have no child_ns_type, so the op is never
called there -- which is why only netdev-anchored portal dirs crashed.
Two parts:
- Create portal dirs with a dedicated kobj_type (portal_dir_ktype) that provides
a non-NULL .namespace returning the creating task's net ns, plus .sysfs_ops =
kobj_sysfs_ops (so kobj_attribute files created inside still dispatch
show/store) and a .release that frees the kobject.
- Look the dir up with the matching namespace (portal_child_ns), so an existing
net-namespaced child (e.g. an interface dir) is HOOKED instead of failing the
ns==NULL lookup and then hitting -EEXIST on create.
Enables creating sysfs subdirectories (and attribute files) under an existing
netdev kobject via the portal. Verified: create + read + write of attributes under
a netdev-anchored directory now works with no Oops.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
handle_op_sysfs_create_or_lookup_dir()creates directories withkobject_create_and_add(), whose kobject usesdynamic_kobj_ktype— which has aNULL
.namespaceop.That is fine under most parents, but when the new directory's parent is
network-namespaced — anything under
/sys/class/net/<if>or/sys/devices/virtual/net/<if>, where the parent ktype'schild_ns_typereportsKOBJ_NS_TYPE_NET— the kernel'screate_dir() -> kobject_namespace()path callskobj->ktype->namespace(kobj)unconditionally. With a NULL op this is aNULL-pointer dereference:
It runs in the context of whatever task triggered the portal op and was observed
landing on PID 1 (
preinit), killing the boot. Non-net parents (e.g./sys/bus/usb, other/sys/class/*) have nochild_ns_type, so the op is neverinvoked there — which is why only netdev-anchored portal dirs crashed.
A second, related issue: the "hook an existing dir" lookup used
ns == NULL, whichmisses a net-namespaced existing child (e.g. an interface directory). The portal
then fell through to creating it and hit
-EEXIST.Fix
kobj_type(portal_dir_ktype) thatprovides a non-NULL
.namespacereturning the creating task's network namespace,plus
.sysfs_ops = kobj_sysfs_ops(sokobj_attributefiles created inside stilldispatch
show/store) and a.releasethat frees the kobject.portal_child_ns) so anexisting net-namespaced child is hooked instead of failing the
ns == NULLlookup and then hitting
-EEXISTon create.Version-guarded
.namespacesignature (constkobjectfrom v6.2).Effect
Creating sysfs subdirectories (and attribute files) under an existing netdev
kobject via the portal now works. Verified on a 6.13 mipsel guest: create + read +
write of attributes under a netdev-anchored directory, with the show/store
callbacks dispatching correctly and no Oops (previously 5 Oopses / boot,
sometimes fatal on PID 1).