Add kdbus implementation
Subsystems (5)
Quoted 6 lines
Quoted 3 lines
The core calls are already mediated by LSM today, right? We don't want anyone to be parsing the data stream through an LSM, that idea got rejected a long time ago as something that is really not a good idea.
Parsing data is out of question, of course, but this is not what we were proposing.
Why is it out of the question. If it's a socket you can just a BPF filter on it, so why can't kdbus support similar basic functionality ?
Alan
How Not To Use kref (was Re: kdbus: add code for buses, domains and endpoints)
In reply to Greg Kroah-Hartmandrivers/misc/kdbus/domain.c line 442
Quoted 9 lines · Greg Kroah-Hartman
drivers/misc/kdbus/domain.c new file +9
-0+static void __kdbus_domain_user_free (struct kref *kref) +{ +struct kdbus_domain_user *user = + container_of(kref, struct kdbus_domain_user, kref); + +BUG_ON (atomic_read (&user->buses) >0 ); +BUG_ON (atomic_read (&user->connections) >0 ); + +mutex_lock (&user->domain->lock);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
drivers/misc/kdbus/domain.c line 444
Quoted 2 lines
drivers/misc/kdbus/domain.c new file +2
-0+idr_remove (&user->domain->user_idr, user->idr); +hash_del (&user->hentry);
^^^^^^^^^^^^^^^^^^^^^^^^
drivers/misc/kdbus/domain.c line 477
Quoted 12 lines
drivers/misc/kdbus/domain.c new file +11
-0+mutex_unlock (&user->domain->lock); + +kdbus_domain_unref (user->domain); +kfree (user); +} +struct kdbus_domain_user *kdbus_domain_user_unref (struct kdbus_domain_user *u) +{ +if (u) +kref_put (&u->kref, __kdbus_domain_user_free); +return NULL ; +}
If you remove an object from some search structures, taking the lock in destructor is Too Fucking Late(tm). Somebody might have already found that puppy and decided to pick it (all under that lock) just as we'd got to that point in destructor and blocked there. Oops...
Normally I'd say "just use kref_put_mutex()", but this case is even worse. Look:
refcount is 1
A: kref_put_mutex()
see that it's potential 1->0 crossing, need to take mutex mutex_lock()
B: kref_get()
refcount is 2
A: got the sodding mutex
atomic_dec_and_test
refcount is 1 now
OK, it's not 1->0, after all, just drop the mutex and bugger off B: kref_put_mutex() see that it's potential 1->0 crossing, need to take mutex mutex_lock() blocks
A: mutex_unlock() lets B go
B: ... got it
atomic_dec_and_test
refcount is 0
call the destructor now, which ends with kdbus_domain_unref(user->domain); ... which just happens to be the last reference to ->domain ... and frees it, along with ->domain->mutex
But what's to guarantee that A will be past the last point where mutex_unlock() is looking at the mutex? Sure, it's hard to hit, but AFAICS it's not impossible, especially if the following happens (assuming mutex-dec.h-style mutices):
B: mutex_lock()
atomic_dec_return -> -1 __mutex_lock_slowpath() A: mutex_unlock() atomic_inc_return -> 0 get preempted
B: note that A has already incremented it to 0 and bugger off - we'd got it
and there we go, with A getting the timeslice back and deciding to call __mutex_unlock_slowpath() when B has already freed the damn thing.
Basically, kref_put_mutex() is only usable when destructor callback cannot end up freeing the mutex.
kref_get_unless_zero() might be a usable approach, but IMO the whole thing is simply outside of kref applicability. Using it for something that needs to deal with removal from search structures from the destructor callback is already stretching the things; this one is far worse. kref isn't a universal tool for expressing lifetime cycles. It works for really simple cases and might eliminate some amount of boilerplate code. It's been greatly oversold and overused, though...
Quoted 25 lines · Karol Lewandowski
Quoted 2 lines · Greg Kroah-Hartman
Other than that, I don't know exactly what your patches do, or why they are needed, care to go into details?
Patches in question were supposed to add few hooks for kdbus-specific operations that doesn't seem to have compatible semantics with hooks currently available in LSM.
kdbus' bus introduces quite a few new concepts that we wanted to be able to limit based on MAC label/context, eg.
- check flags at HELO stage (say disallow fd passing),
- restrict ability to acquire name to certain subjects (for system bus),
- disallow creation of new buses,
- limit scope of broadcasts,
- etc.
Please take a look at hook list - I think most of names are self-explanatory:
Quoted 5 lines
https://github.com/lmctl/linux/blob/a9fe4c33b6e5ab25a243e0590df406aabb6add1
2/include/linux/security.h#L1874kdbus modifications were pretty light - with most visible change being addition of opaque security pointer to
kdbus_busand similar structs.
[NOTE: we really should add the LSM list to this discussion and future patchset postings.]
Also, to be completely honest, I don't think we ever really arrived at any final conclusion about those LSM/kdbus hooks either. At least I don't think I ever really satisfied myself that what we had was the "right" solution.
We both got busy and kinda drifted away from this effort. Karol, did you do any further work on the hooks?
Signature · 2 lines
paul moore security and virtualization @ redhat
Re: kdbus: add driver skeleton, ioctl entry points and utility functions
In reply to Greg Kroah-HartmanQuoted 4 lines · Greg Kroah-Hartman
drivers/misc/kdbus/handle.c new file +4
-0+/* kdbus major */ +static unsigned int kdbus_major; + +/* map of minors to objects */
drivers/misc/kdbus/handle.c line 107
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+static DEFINE_IDR (kdbus_minor_idr); + +/* kdbus minor lock */ +static DEFINE_SPINLOCK (kdbus_minor_lock); + +int kdbus_minor_init (void ) +{ +int ret; + + ret = __register_chrdev(0, 0, 0xfffff, KBUILD_MODNAME,
0xfffff? Random number pulled out of thin air or is there some sensible explanation for this choice?
drivers/misc/kdbus/handle.c line 112
Quoted 5 lines
drivers/misc/kdbus/handle.c new file +5
-0+ &kdbus_handle_ops); +if (ret <0 ) +return ret; + + kdbus_major = ret;
So minor_init actually assigns the major number ...
drivers/misc/kdbus/handle.c line 118
Quoted 6 lines
drivers/misc/kdbus/handle.c new file +6
-0+return 0 ; +} + +void kdbus_minor_exit (void ) +{ +__unregister_chrdev (kdbus_major,0 ,0xfffff ,KBUILD_MODNAME );
So we have the magic 0xfffff constant at two places to make sure that it is updated in sync.
drivers/misc/kdbus/handle.c line 126
Quoted 8 lines
drivers/misc/kdbus/handle.c new file +8
-0+idr_destroy (&kdbus_minor_idr); +} + +static void *kdbus_minor_pack (enum kdbus_minor_type type,void *ptr) +{ +unsigned long p = (unsigned long )ptr; + +BUILD_BUG_ON (KDBUS_MINOR_CNT >4 );
We certainly want a build bug in some random function with another magic number for a completely undocumented enum, which does neither explain what its enum constants mean nor does have a comment that it is limited to 0-3 for a very good reason.
And of course any not overloaded pointer defaults to KDBUS_MINOR_CONTROL which is always valid...
Review
Quoted 2 lines
(quoted diff) +2
-0+ + if (WARN_ON(p & 0x3UL || type >= KDBUS_MINOR_CNT))
0x03UL is a very descriptive constant ....
drivers/misc/kdbus/handle.c line 139
Quoted 11 lines
drivers/misc/kdbus/handle.c new file +11
-0+return NULL ; + +return (void *)(p | (unsigned long )type); +} + +static enum kdbus_minor_type kdbus_minor_unpack (void **ptr) +{ +unsigned long p = (unsigned long )*ptr; + + *ptr = (void *)(p & ~0x3UL ); +return p &0x3UL ;
I'm really excited about the intuitive naming conventions here. minor_init() initializes kdbus_major and this pack/unpack stuff converts a pointer to carry a type and vice versa. Of course that stuff lacks any comment in order to accelerate reviews, right?
I really had to look more than twice to figure out that this function serves two purposes;
- Remove the type overload from the pointer
- Return the type retrieved from the pointer.
Aside of that: What on earth has minor to do with this?
For heavens sake. Minor is referring to a minor number in the context of character devices, right?
And a number does not map at all to a randomly overloaded pointer AFAICT.
Quoted 2 lines
+/**
+ *kdbus_minor_set()- set an existing minor type of a kdbus device node
Groan. The name choices are just ass backwards to be honest. And the explanation of the function is even worse:
"set an existing minor type of a kdbus device node"
What the heck is an 'existing minor type' ?
Ah, you mean if the type does not exist, i.e. it is >= KDBUS_MINOR_CNT) then the idr entry will be replaced by a NULL pointer silently.
Aside of that if ptr has one of the lower two bits set, and the call tries to change its type then we get a WARN_ON in dmesg and happily assign a NULL pointer to the idr entry for that minor number.
Makes a lot of sense. We'll see the fallout later...
Quoted 1 line
+ * @devt: The device node to remove
So @devt is removed? The documentation of idr_replace() tells a different story.
Quoted 2 lines
+ * @type: New type to set
+ * @ptr: Associated pointer when node was initially registered
Why is this a void pointer? Are we dealing with arbitrary node types here?
drivers/misc/kdbus/handle.c line 230
Quoted 8 lines
+ */
drivers/misc/kdbus/handle.c new file +7
-0+void kdbus_minor_set (dev_t devt,enum kdbus_minor_type type,void *ptr) +{ +unsigned int minor =MINOR (devt); + + ptr =kdbus_minor_pack (type, ptr); + +spin_lock (&kdbus_minor_lock);
Why is this a spinlock and not a mutex?
drivers/misc/kdbus/handle.c line 231
Quoted 1 line
drivers/misc/kdbus/handle.c new file +1
-0+ ptr =idr_replace (&kdbus_minor_idr, ptr, minor);
What's the value of this pointless assignment? Pacify gcc?
Quoted 17 lines
drivers/misc/kdbus/handle.c new file +17
-0+static int kdbus_handle_release (struct inode *inode,struct file *file) +{ +struct kdbus_handle *handle = file->private_data; + +switch (handle->type) { +case KDBUS_HANDLE_CONTROL_DOMAIN_OWNER : +kdbus_domain_disconnect (handle->domain_owner); +kdbus_domain_unref (handle->domain_owner); +break ; + +case KDBUS_HANDLE_CONTROL_BUS_OWNER : +kdbus_bus_disconnect (handle->bus_owner); +kdbus_bus_unref (handle->bus_owner); +break ; + +case KDBUS_HANDLE_ENDPOINT_OWNER : +kdbus_ep_disconnect (handle->ep_owner);
drivers/misc/kdbus/handle.c line 349
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+kdbus_ep_unref (handle->ep_owner); +break ; + +case KDBUS_HANDLE_ENDPOINT_CONNECTED : +kdbus_conn_disconnect (handle->conn, false); +kdbus_conn_unref (handle->conn); +break ; + +default : +break ;
Silent acceptance of type being unknown?
drivers/misc/kdbus/handle.c line 365
Quoted 6 lines
drivers/misc/kdbus/handle.c new file +6
-0+static int kdbus_copy_from_user (void *dest, +void __user *user_ptr, +size_t size) +{ +if (!KDBUS_IS_ALIGNED8 ((uintptr_t )user_ptr)) +return -EFAULT ;
Completely undocumented requirement and there is no reason WHY we need KDBUS_IS_ALIGNED8 to figure that out ....
Quoted 4 lines
drivers/misc/kdbus/handle.c new file +4
-0+static int kdbus_handle_transform (struct kdbus_handle *handle, +enum kdbus_handle_type old_type, +enum kdbus_handle_type new_type, +void *ctx_ptr)
drivers/misc/kdbus/handle.c line 413
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+{ +int ret = -EBADFD ; + +/* +* This transforms a handle from one state into another. Only a single +* transformation is allowed per handle, and it must be one of: +* CONTROL -> CONTROL_DOMAIN_OWNER +* -> CONTROL_BUS_OWNER +* EP -> EP_CONNECTED +* -> EP_OWNER
And that's magically enforced by what? new_type is not sanity checked here at all and there is no requirement for the call site to do so.
Quoted 14 lines
drivers/misc/kdbus/handle.c new file +14
-0+/* kdbus control device commands */ +static long kdbus_handle_ioctl_control (struct file *file,unsigned int cmd, +void __user *buf) +{ +struct kdbus_handle *handle = file->private_data; +struct kdbus_bus *bus =NULL ; +struct kdbus_cmd_make *make; +struct kdbus_domain *domain =NULL ; +umode_t mode =0600 ; +void *p =NULL ; +int ret; + +switch (cmd) { +case KDBUS_CMD_BUS_MAKE : {
drivers/misc/kdbus/handle.c line 460
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+kgid_t gid =KGIDT_INIT (0 ); +struct kdbus_bloom_parameter bloom; +char *name; + + ret =kdbus_memdup_user (buf, &p,sizeof (*make), +KDBUS_MAKE_MAX_SIZE ); +if (ret <0 ) +break ; + + make = p;
Another great coding convention stolen from some ramdonly misdesigned user space app?
ret = kdbus_memdup_user(buf, &p, sizeof(*make)....
What's wrong with
struct kdbus_cmd_make *make = NULL;
ret = kdbus_memdup_user(buf, &make, sizeof(*make) ... ????Surely void pointers are a great guarantee to make things better, right?
drivers/misc/kdbus/handle.c line 505
Quoted 4 lines
drivers/misc/kdbus/handle.c new file +4
-0+case KDBUS_CMD_DOMAIN_MAKE : { +const char *name; + +if (!capable (CAP_IPC_OWNER )) {
Why is this not in the open() call? Because you have an opaque device at the time of open()?
Offloading security relevant decisions to an ioctl is an interesting design choice in theory. In fact it is just wrong.
drivers/misc/kdbus/handle.c line 515
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+ ret = -EPERM ; +break ; + } + + ret =kdbus_memdup_user (buf, &p,sizeof (*make), +KDBUS_MAKE_MAX_SIZE ); +if (ret <0 ) +break ; + + make = p;
See above.
Quoted 8 lines
drivers/misc/kdbus/handle.c new file +8
-0+/* kdbus endpoint make commands */ +static long kdbus_handle_ioctl_ep (struct file *file,unsigned int cmd, +void __user *buf) +{ +struct kdbus_handle *handle = file->private_data; +void *p =NULL ; +long ret =0 ; +
drivers/misc/kdbus/handle.c line 581
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+switch (cmd) { +case KDBUS_CMD_ENDPOINT_MAKE : { +struct kdbus_cmd_make *make; +umode_t mode =0 ; +kgid_t gid =KGIDT_INIT (0 ); +const char *name; +struct kdbus_ep *ep; + +/* creating custom endpoints is a privileged operation */ +if (!kdbus_bus_uid_is_privileged (handle->ep->bus)) {
See above.
drivers/misc/kdbus/handle.c line 515
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+ ret = -EPERM ; +break ; + } + + ret =kdbus_memdup_user (buf, &p,sizeof (*make), +KDBUS_MAKE_MAX_SIZE ); +if (ret <0 ) +break ; + + make = p;
See above.
drivers/misc/kdbus/handle.c line 665
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+case KDBUS_CMD_HELLO : { +struct kdbus_cmd_hello *hello; +struct kdbus_conn *conn =NULL ; + + ret =kdbus_memdup_user (buf, &p,sizeof (*hello), +KDBUS_HELLO_MAX_SIZE ); +if (ret <0 ) +break ; + + hello = p;
Ditto.
Quoted 8 lines
drivers/misc/kdbus/handle.c new file +8
-0+/* kdbus endpoint commands for connected peers */ +static long kdbus_handle_ioctl_ep_connected (struct file *file,unsigned int cmd, +void __user *buf) +{ +struct kdbus_handle *handle = file->private_data; +struct kdbus_conn *conn = handle->conn; +void *p =NULL ; +long ret =0 ;
drivers/misc/kdbus/handle.c line 733
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+ +/* +* BYEBYE is special; we must not acquire a connection when +* calling into kdbus_conn_disconnect() or we will deadlock, +* because kdbus_conn_disconnect() will wait for all acquired +* references to be dropped. +*/ +if (cmd ==KDBUS_CMD_BYEBYE ) { +if (!kdbus_conn_is_connected (conn)) +return -EOPNOTSUPP ;
If the connection is down already then a BEYBEY is just moot. I don't see a good reason WHY the return code is -EOPNOTSUPP.
Is this just to provide bug compability with the existing user space code? If so, pick some proper return code which reflects the state and deal with it in user space. If not, then you should think hard why you did not find anything which is more appropriate in the wide choice of error codes.
Quoted 4 lines
drivers/misc/kdbus/handle.c new file +4
-0+return kdbus_conn_disconnect (conn, true); + } + + ret =kdbus_conn_acquire (conn);
drivers/misc/kdbus/handle.c line 748
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+if (ret <0 ) +return ret; + +switch (cmd) { +case KDBUS_CMD_NAME_ACQUIRE : { +/* acquire a well-known name */ +struct kdbus_cmd_name *cmd_name; + +if (!kdbus_conn_is_connected (conn)) { + ret = -EOPNOTSUPP ;
See above.
Review
Quoted 2 lines
(quoted diff) +2
-0+ break; + }
Aside of that what makes sure that the connection is not going away after you checked it above?
Magic serialization or what? I can't see any of it.
If it's just an optimization then it wants to have a proper comment and not just a random chosen return code which matches the expectation of some equally undocumented user space app.
drivers/misc/kdbus/handle.c line 1150
Quoted 9 lines
drivers/misc/kdbus/handle.c new file +9
-0+static long kdbus_handle_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +struct kdbus_handle *handle = file->private_data; +void __user *argp = (void __user *)arg; +enum kdbus_handle_type type = handle->type; + +/* make sure all handle fields are set if handle->type is */ +smp_rmb ();
Sure. You really need this kind of serialization because your design choice of allowing opaque handles in the first place.
I'm really interested why you need this rmb() at all. Just because you have several threads in user space which might race with the type assignment when they call the ioctl?
We have a strict requirement to document memory barriers. The following comment definitely does not fulfil this requirement as it just documents that someone observed a race of unknown provenance and got it 'fixed' with a 'smp_rmb()'
Quoted 1 line
+ /* make sure all handle fields are set if
handle->typeis */
That's really hillarious, The user space side knows excatly upfront which type of 'handle' it wants to open. Making it an opaque handle in the first place and let the kernel deal with the actual type assignment is beyond silly. Especially if that involves undocumented memory barriers.
Quoted 21 lines
drivers/misc/kdbus/handle.c new file +21
-0+switch (type) { +case KDBUS_HANDLE_CONTROL : +return kdbus_handle_ioctl_control (file, cmd, argp); + +case KDBUS_HANDLE_EP : +return kdbus_handle_ioctl_ep (file, cmd, argp); + +case KDBUS_HANDLE_ENDPOINT_CONNECTED : +return kdbus_handle_ioctl_ep_connected (file, cmd, argp); + +case KDBUS_HANDLE_ENDPOINT_OWNER : +return kdbus_handle_ioctl_ep_owner (file, cmd, argp); + +default : +return -EBADFD ; + } +} + +static unsigned int kdbus_handle_poll (struct file *file, +struct poll_table_struct *wait) +{
drivers/misc/kdbus/handle.c line 1182
Quoted 10 lines
drivers/misc/kdbus/handle.c new file +10
-0+struct kdbus_handle *handle = file->private_data; +struct kdbus_conn *conn; +unsigned int mask =POLLOUT |POLLWRNORM ; + +/* Only a connected endpoint can read/write data */ +if (handle->type !=KDBUS_HANDLE_ENDPOINT_CONNECTED ) +return POLLERR |POLLHUP ; + +/* make sure handle->conn is set if handle->type is */ +smp_rmb ();
Surely we need to plaster that all over the place just because we avoid to open dedicated devices in the first place. And do not tell me that the open call does not know what type it is going to be.
Copying badly designed userspace code to the kernel without rethinking the design and 'fixing' the short comings by copying the same 'fixup' over and over is definitely a guarantee for interesting CVEs in the future.
Thanks,
tglx
Re: kdbus: add driver skeleton, ioctl entry points and utility functions
Quoted 22 lines · Thomas Gleixner
drivers/misc/kdbus/handle.c line 1150
Quoted 9 lines
drivers/misc/kdbus/handle.c new file +9
-0+static long kdbus_handle_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +struct kdbus_handle *handle = file->private_data; +void __user *argp = (void __user *)arg; +enum kdbus_handle_type type = handle->type; + +/* make sure all handle fields are set if handle->type is */ +smp_rmb ();Sure. You really need this kind of serialization because your design choice of allowing opaque handles in the first place.
I'm really interested why you need this
rmb()at all. Just because you have several threads in user space which might race with the type assignment when they call the ioctl?We have a strict requirement to document memory barriers. The following comment definitely does not fulfil this requirement as it just documents that someone observed a race of unknown provenance and got it 'fixed' with a '
smp_rmb()'
Quoted 7 lines
Quoted 1 line
+ /* make sure all handle fields are set if
handle->typeis */That's really hillarious, The user space side knows excatly upfront which type of 'handle' it wants to open. Making it an opaque handle in the first place and let the kernel deal with the actual type assignment is beyond silly. Especially if that involves undocumented memory barriers.
I have been staring at exactly this for rather a long time today.
Apparently this barrier pairs with smp_wmb() in kdbus_handle_transform() and tries to make sure that whenever handle->type is seen as updated, handle->ptr is as well.
But it's still difficult for me to understand all the memory ordering rules and consequences of this strict ordering (my current understanding is that the barrier is not needed, but I will have to think about it a little bit more), so a nice and explanatory comment precisely describing the race this is protecting against would be very welcome.
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Re: kdbus: add driver skeleton, ioctl entry points and utility functions
Quoted 31 lines · Jiri Kosina
Quoted 22 lines · Thomas Gleixner
drivers/misc/kdbus/handle.c line 1150
Quoted 9 lines
drivers/misc/kdbus/handle.c new file +9
-0+static long kdbus_handle_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) +{ +struct kdbus_handle *handle = file->private_data; +void __user *argp = (void __user *)arg; +enum kdbus_handle_type type = handle->type; + +/* make sure all handle fields are set if handle->type is */ +smp_rmb ();Sure. You really need this kind of serialization because your design choice of allowing opaque handles in the first place.
I'm really interested why you need this
rmb()at all. Just because you have several threads in user space which might race with the type assignment when they call the ioctl?We have a strict requirement to document memory barriers. The following comment definitely does not fulfil this requirement as it just documents that someone observed a race of unknown provenance and got it 'fixed' with a '
smp_rmb()'Quoted 7 lines
Quoted 1 line
+ /* make sure all handle fields are set if
handle->typeis */That's really hillarious, The user space side knows excatly upfront which type of 'handle' it wants to open. Making it an opaque handle in the first place and let the kernel deal with the actual type assignment is beyond silly. Especially if that involves undocumented memory barriers.
Quoted 5 lines
I have been staring at exactly this for rather a long time today.
Apparently this barrier pairs with
smp_wmb()inkdbus_handle_transform()and tries to make sure that wheneverhandle->typeis seen as updated,handle->ptris as well.
Right. But it does not make any sense at all.
The underlying problem is the design of the whole character device interface as an opaque type. Just look at the absurd workarounds in the userspace implementation of this,
Now we copy it to kernel space 1:1 and find other absurd workarounds for it instead of designing it new.
Welcome to the world of bug compability...
Thanks,
tglx
Re: kdbus: add code for buses, domains and endpoints
In reply to Greg Kroah-HartmanQuoted 1 line · Greg Kroah-Hartman
See
Documentation/kdbus.txtfor more details.
... which has nothing whatsoever on object lifetime rules. Could you folks please document that somewhere? What pins what, what state transitions are possible, etc.
BTW, the calling conventions for your foo_new() are annoying - instead of "return -E... or 0, storing the reference to new object in var parameter passed as the last argument", could you please just return ERR_PTR(-E...) on error, a pointer to new object on success and to hell with those struct foo **foo in the argument lists?
Re: kdbus: add code for buses, domains and endpoints
Hi,
Quoted 4 lines · Al Viro
Quoted 1 line · Greg Kroah-Hartman
See
Documentation/kdbus.txtfor more details.... which has nothing whatsoever on object lifetime rules.
True. That document only describes the external API exposed by the driver towards userspace.
Quoted 3 lines
Could you
folks please document that somewhere? What pins what, what state transitions are possible, etc.
Hmm, I'll see whether I can write something up.
Quoted 5 lines
BTW, the calling conventions for your
foo_new()are annoying - instead of "return -E... or 0, storing the reference to new object in var parameter passed as the last argument", could you please just returnERR_PTR(-E...)on error, a pointer to new object on success and to hell with those struct foo **foo in the argument lists?
No problem at all. We'll change that around.
Thanks for your feedback, much appreciated!
Daniel
Re: kdbus: add header file
In reply to Arnd BergmannQuoted 5 lines · Arnd Bergmann
Quoted 2 lines · On Thursday 30 October 2014 12:52:58 Daniel Mack wrote:
Hmm, this is the header exported to userspace, so having enums in would make our lives easier, right?
Quoted 8 lines
My point was that you never use the enum by type and the only place in user space where it's referenced would be something like
ret = ioctl(fd, KDBUS_CMD_BUS_MAKE, &make);In the debugger, you will see the source line here. If you trace into the glibc ioctl function, you no longer know the type because that just has an 'int'.
Alright - I changed that to #defines now.
Thanks,
Daniel
Quoted 9 lines · One Thousand Gnomes
Quoted 6 lines
Quoted 3 lines
The core calls are already mediated by LSM today, right? We don't want anyone to be parsing the data stream through an LSM, that idea got rejected a long time ago as something that is really not a good idea.
Parsing data is out of question, of course, but this is not what we were proposing.
Why is it out of the question. If it's a socket you can just a BPF filter on it, so why can't kdbus support similar basic functionality ?
Fair point. I think that none of us simply considered this till now.
Thanks
Signature · 1 line
Karol Lewandowski, Samsung R&D Institute Poland
Quoted 56 lines · Greg Kroah-Hartman
Quoted 22 lines · Karol Lewandowski
Quoted 21 lines · Greg Kroah-Hartman
Quoted 19 lines · Karol Lewandowski
[ Sorry for breaking thread and resend - gmane rejected my original message
due to too long list of recipients... ]Quoted 5 lines · Greg Kroah-Hartman
There is a 1815 line documentation file in this series, so we aren't trying to not provide this type of information here at all. But yes, more background, about why this can't be done in userspace (zero copy, less context switches, proper credential passing, timestamping, availble at early-boot, LSM hooks for security models to tie into
While you're at it... I did some work on proof-of-concept LSM patches for kdbus some time ago, see [1][2]. Currently, these are completely of date.
[1] https://github.com/lmctl/linux/commits/kdbus-lsm-v4.for-systemd-v212
[2] https://github.com/lmctl/kdbus/commit/aa0885489d19be92fa41c6f0a71df28763228a40May I ask if you guys have your own plan for LSM or maybe it would be worth to resurrect [1]?
Quoted 6 lines
Quoted 3 lines
The core calls are already mediated by LSM today, right? We don't want anyone to be parsing the data stream through an LSM, that idea got rejected a long time ago as something that is really not a good idea.
Parsing data is out of question, of course, but this is not what we were proposing.
Glad to hear it :)
Quoted 12 lines
Quoted 2 lines
Other than that, I don't know exactly what your patches do, or why they are needed, care to go into details?
Patches in question were supposed to add few hooks for kdbus-specific operations that doesn't seem to have compatible semantics with hooks currently available in LSM.
kdbus' bus introduces quite a few new concepts that we wanted to be able to limit based on MAC label/context, eg.
- check flags at HELO stage (say disallow fd passing),
Quoted 7 lines
- restrict ability to acquire name to certain subjects (for system bus),
- disallow creation of new buses,
- limit scope of broadcasts,
- etc.
Nice list.
Quoted 2 lines
Please take a look at hook list - I think most of names are self-explanatory:
Quoted 8 lines
Quoted 4 lines
kdbus modifications were pretty light - with most visible change being addition of opaque security pointer to
kdbus_busand similar structs.That looks very reasonable, care to make it up into a patch I can add to the end of this series so it's easy to review and possibly submit as part of it?
I'll do my best to prepare something suitable for review, but I'm not sure it can/should be part of next patch set.
As Paul wrote - discussion about hooks hasn't really ended up with satisfactory conclusion but just faded away. kdbus own policy engine has been rewritten since I last touched it so I'm not sure what part are still applicable.
(Unfortunately, I'll be traveling from monday and likely to be offline for a week or two...)
Thanks
Signature · 1 line
Karol Lewandowski, Samsung R&D Institute Poland
Quoted 32 lines · Paul Moore
Quoted 25 lines · Karol Lewandowski
Quoted 2 lines · Greg Kroah-Hartman
Other than that, I don't know exactly what your patches do, or why they are needed, care to go into details?
Patches in question were supposed to add few hooks for kdbus-specific operations that doesn't seem to have compatible semantics with hooks currently available in LSM.
kdbus' bus introduces quite a few new concepts that we wanted to be able to limit based on MAC label/context, eg.
- check flags at HELO stage (say disallow fd passing),
- restrict ability to acquire name to certain subjects (for system bus),
- disallow creation of new buses,
- limit scope of broadcasts,
- etc.
Please take a look at hook list - I think most of names are self-explanatory:
Quoted 5 lines
https://github.com/lmctl/linux/blob/a9fe4c33b6e5ab25a243e0590df406aabb6add1
2/include/linux/security.h#L1874kdbus modifications were pretty light - with most visible change being addition of opaque security pointer to
kdbus_busand similar structs.
Quoted 6 lines
[NOTE: we really should add the LSM list to this discussion and future patchset postings.]
Also, to be completely honest, I don't think we ever really arrived at any final conclusion about those LSM/kdbus hooks either. At least I don't think I ever really satisfied myself that what we had was the "right" solution.
Agreed, "hooks" are far from being complete. I think that patches were and still are - a starting point for discussion, not "a solution" itself.
Timing wasn't good either - since our last discussion (Apr/May 2014) kdbus policy engine has been completely rewritten and few core concepts changed too.
Quoted 2 lines
We both got busy and kinda drifted away from this effort. Karol, did you do any further work on the hooks?
I didn't. I was waiting for the peace of change in kdbus to slow down a bit and, honestly, wasn't expecting submission in few next months...
I'll do my best to post RFC patchset today or tomorrow.
Thanks
Signature · 1 line
Karol Lewandowski, Samsung R&D Institute Poland
cover kdbus: add support for lsm
This is set of EXPERIMENTAL patches adding lsm support to kdbus. (Rebased on top of v3.17.)
Quoted 1 line
From least to most invasive:
- (1) kdbus: extend structures with security pointer for lsm
Trivial. Applicable as-is.
- (2) security: export security_file_receive for modules
(3) kdbus: check if lsm permits installing received fds
fd_install doesn't seem to consult LSM, these patches ensure that receiving process has the right to sent fds.
Compile-tested only.
- (4) security: introduce lsm hooks for kdbus
(5) kdbus: make use of new lsm hooks
Set of proof-of-concept hooks discussed previously with Paul Moore.
kdbus integration patch (5) for review, but unlikely for integration at this stage.
Likewise, compile-tested only.
Karol Lewandowski (5):
kdbus: extend structures with security pointer for lsm
security: export security_file_receive for modules
kdbus: check if lsm permits installing received fds
security: introduce lsm hooks for kdbus
kdbus: make use of new lsm hooks
drivers/misc/kdbus/bus.c | 10 +++- drivers/misc/kdbus/bus.h | 2 + drivers/misc/kdbus/connection.c | 34 +++++++++++- drivers/misc/kdbus/connection.h | 2 + drivers/misc/kdbus/domain.c | 7 +++ drivers/misc/kdbus/domain.h | 2 + drivers/misc/kdbus/endpoint.c | 11 ++++ drivers/misc/kdbus/names.c | 9 ++++ drivers/misc/kdbus/queue.c | 13 +++++ include/linux/security.h | 114 ++++++++++++++++++++++++++++++++++++++++ security/capability.c | 84 +++++++++++++++++++++++++++++ security/security.c | 85 ++++++++++++++++++++++++++++++ 12 files changed, 371 insertions(+), 2 deletions(-)
Signature · 1 line
2.1.1
PATCH 1/5 kdbus: extend structures with security pointer for lsm
--- drivers/misc/kdbus/bus.h | 2 ++ drivers/misc/kdbus/connection.h | 2 ++ drivers/misc/kdbus/domain.h | 2 ++ 3 files changed, 6 insertions(+)
drivers/misc/kdbus/bus.h +2 -0
@@ -49,6 +49,7 @@* @conn_hash: Map of connection IDs * @monitors_list: Connections that monitor this bus * @meta: Meta information about the bus creator +* @security: LSM security blob * * A bus provides a "bus" endpoint / device node. *
@@ -84,6 +85,7 @@ struct kdbus_bus {struct list_head monitors_list;struct kdbus_meta *meta; +void *security; }; int kdbus_bus_make_user(const struct kdbus_cmd_make *make,
drivers/misc/kdbus/connection.h +2 -0
@@ -64,6 +64,7 @@* waits for replies from the peer * @wait: Wake up this endpoint * @queue: The message queue associcated with this connection +* @security: LSM security blob */ struct kdbus_conn {struct kref kref;
@@ -98,6 +99,7 @@ struct kdbus_conn {atomic_t reply_count;wait_queue_head_t wait;struct kdbus_queue queue; +void *security; };struct kdbus_kmsg ;
drivers/misc/kdbus/domain.h +2 -0
@@ -35,6 +35,7 @@* @bus_list: Buses in this domain * @user_hash: Accounting of user resources * @user_idr: Map of all users; smallest possible index +* @security: LSM security blob * * A domain provides a "control" device node. Every domain has its * own major number for its endpoint device nodes.
@@ -62,6 +63,7 @@ struct kdbus_domain {struct list_head bus_list;DECLARE_HASHTABLE (user_hash,6 );struct idr user_idr; +void *security; };/**
Signature · 1 line
2.1.1
PATCH 2/5 security: export security_file_receive for modules
In reply to Karol LewandowskiThis is required for kdbus built as module.
--- security/security.c | 1 + 1 file changed, 1 insertion(+)
security/security.c +1 -0
@@ -790,6 +790,7 @@ int security_file_receive(struct file *file) {return security_ops->file_receive (file); } +EXPORT_SYMBOL (security_file_receive);int security_file_open (struct file *file,const struct cred *cred) {
Signature · 1 line
2.1.1
PATCH 3/5 kdbus: check if lsm permits installing received fds
In reply to Karol LewandowskiFrom: Karol Lewandowski <[email protected]>
--- drivers/misc/kdbus/queue.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/misc/kdbus/queue.c +13 -0
@@ -25,6 +25,7 @@#include <linux/poll.h> #include <linux/sched.h> #include <linux/sizes.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/syscalls.h>
@@ -41,6 +42,18 @@ static int kdbus_queue_entry_fds_install(struct kdbus_queue_entry *entry)int ret, *fds;size_t count; +for (i =0 ; i < entry->fds_count; i++) { + ret =security_file_receive (entry->fds_fp[i]); +if (ret) +return ret; + } + +for (i =0 ; i < entry->memfds_count; i++) { + ret =security_file_receive (entry->memfds_fp[i]); +if (ret) +return ret; + } +/* get array of file descriptors */ count = entry->fds_count + entry->memfds_count;if (!count)
Signature · 1 line
2.1.1
PATCH 4/5 security: introduce lsm hooks for kdbus
In reply to a message not in the archiveThis is proof-of-concept set of hooks for kdbus by Karol Lewandowski and Paul Moore.
--- include/linux/security.h | 114 +++++++++++++++++++++++++++++++++++++++++++++++ security/capability.c | 84 ++++++++++++++++++++++++++++++++++ security/security.c | 84 ++++++++++++++++++++++++++++++++++ 3 files changed, 282 insertions(+)
include/linux/security.h +114 -0
@@ -53,6 +53,10 @@ struct msg_queue;struct xattr ;struct xfrm_sec_ctx ;struct mm_struct ; +struct kdbus_ep ; +struct kdbus_bus ; +struct kdbus_conn ; +struct kdbus_domain ;/* Maximum number of letters for an LSM name string */ #define SECURITY_NAME_MAX 10
@@ -1438,6 +1442,7 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)* @ctxlen points to the place to put the length of @ctx. * This is the main security structure. */ +/* XXX - need to include descriptions for the kdbus hooks in the block above */ struct security_operations {char name[SECURITY_NAME_MAX +1 ];
@@ -1645,6 +1650,24 @@ struct security_operations {int (*inode_setsecctx)(struct dentry *dentry,void *ctx,u32 ctxlen);int (*inode_getsecctx)(struct inode *inode,void **ctx,u32 *ctxlen); +int (*kdbus_domain_alloc)(struct kdbus_domain *domain); +void (*kdbus_domain_free)(struct kdbus_domain *domain); + +int (*kdbus_bus_alloc)(struct kdbus_bus *bus); +void (*kdbus_bus_free)(struct kdbus_bus *bus); +int (*kdbus_send)(const struct kdbus_conn *conn,const struct kdbus_bus *bus); +int (*kdbus_recv)(const struct kdbus_conn *conn,const struct kdbus_bus *bus); +int (*kdbus_name_acquire)(const struct kdbus_conn *conn,const char *name); +int (*kdbus_name_list)(const struct kdbus_bus *bus); + +int (*kdbus_ep_create)(const struct kdbus_bus *bus); +int (*kdbus_ep_setpolicy)(const struct kdbus_bus *bus); + +int (*kdbus_connect)(struct kdbus_conn *conn,const char *secctx,u32 seclen); +void (*kdbus_conn_free)(struct kdbus_conn *conn); +int (*kdbus_conn_info)(const struct kdbus_conn *conn); +int (*kdbus_talk)(const struct kdbus_conn *src,const struct kdbus_conn *dst); +#ifdef CONFIG_SECURITY_NETWORK int (*unix_stream_connect) (struct sock *sock,struct sock *other,struct sock *newsk);int (*unix_may_send) (struct socket *sock,struct socket *other);
@@ -1905,6 +1928,25 @@ void security_release_secctx(char *secdata, u32 seclen);int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); + +int security_kdbus_domain_alloc (struct kdbus_domain *domain); +void security_kdbus_domain_free (struct kdbus_domain *domain); + +int security_kdbus_bus_alloc (struct kdbus_bus *bus); +void security_kdbus_bus_free (struct kdbus_bus *bus); +int security_kdbus_send (const struct kdbus_conn *conn,const struct kdbus_bus *bus); +int security_kdbus_recv (const struct kdbus_conn *conn,const struct kdbus_bus *bus); +int security_kdbus_name_acquire (const struct kdbus_conn *conn,const char *name); +int security_kdbus_name_list (const struct kdbus_bus *bus); + +int security_kdbus_ep_create (struct kdbus_bus *bus); +int security_kdbus_ep_setpolicy (struct kdbus_bus *bus); + +int security_kdbus_connect (struct kdbus_conn *conn,const char *secctx,u32 seclen); +void security_kdbus_conn_free (struct kdbus_conn *conn); +int security_kdbus_conn_info (const struct kdbus_conn *conn); +int security_kdbus_talk (const struct kdbus_conn *src,const struct kdbus_conn *dst); +#else /* CONFIG_SECURITY */ struct security_mnt_opts { };
@@ -2630,6 +2672,78 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 {return -EOPNOTSUPP ; } + +static inline int security_kdbus_domain_alloc (struct kdbus_domain *domain) +{ +return 0 ; +} +static inline void security_kdbus_domain_free (struct kdbus_domain *domain) +{ +} + +static inline int security_kdbus_bus_alloc (struct kdbus_bus *bus) +{ +return 0 ; +} + +static inline void security_kdbus_bus_free (struct kdbus_bus *bus) +{ +} + +static inline int security_kdbus_send (const struct kdbus_conn *conn, +const struct kdbus_bus *bus) +{ +return 0 ; +} + +static inline int security_kdbus_recv (const struct kdbus_conn *conn, +const struct kdbus_bus *bus) +{ +return 0 ; +} + +static inline int security_kdbus_name_acquire (const struct kdbus_conn *conn, +const char *name) +{ +return 0 ; +} + +static inline int security_kdbus_name_list (const struct kdbus_bus *bus) +{ +return 0 ; +} + +static inline int security_kdbus_ep_create (const struct kdbus_bus *bus) +{ +return 0 ; +} + +static inline int security_kdbus_ep_setpolicy (const struct kdbus_bus *bus) +{ +return 0 ; +} + +static inline int security_kdbus_connect (struct kdbus_conn *conn, +const char *secctx,u32 seclen) +{ +return 0 ; +} + +static inline void security_kdbus_conn_free (struct kdbus_conn *conn) +{ +} + +static inline int security_kdbus_conn_info (const struct kdbus_conn *conn) +{ +return 0 ; +} + +static inline int security_kdbus_talk (const struct kdbus_conn *src, +const struct kdbus_conn *dst) +{ +return 0 ; +} +#endif /* CONFIG_SECURITY */ #ifdef CONFIG_SECURITY_NETWORK
security/capability.c +84 -0
96 lines not on this page · raw message
security/security.c +84 -0
90 lines not on this page · raw message
Signature · 1 line
2.1.1
PATCH 5/5 kdbus: make use of new lsm hooks
In reply to a message not in the archiveDANGEROUS: This version has been compile-tested only!
--- drivers/misc/kdbus/bus.c | 10 +++++++++- drivers/misc/kdbus/connection.c | 34 +++++++++++++++++++++++++++++++++- drivers/misc/kdbus/domain.c | 7 +++++++ drivers/misc/kdbus/endpoint.c | 11 +++++++++++ drivers/misc/kdbus/names.c | 9 +++++++++ 5 files changed, 69 insertions(+), 2 deletions(-)
drivers/misc/kdbus/bus.c +9 -1
@@ -18,6 +18,7 @@#include <linux/module.h> #include <linux/random.h> #include <linux/sched.h> +#include <linux/security.h> #include <linux/sizes.h> #include <linux/slab.h> #include <linux/uaccess.h>
@@ -97,6 +98,7 @@ static void __kdbus_bus_free(struct kref *kref)kdbus_domain_unref (bus->domain);kdbus_policy_db_clear (&bus->policy_db);kdbus_meta_free (bus->meta); +security_kdbus_bus_free (bus);kfree (bus->name);kfree (bus); }
@@ -351,9 +353,13 @@ int kdbus_bus_new(struct kdbus_domain *domain,if (ret <0 ) goto exit_free_name; + ret =security_kdbus_bus_alloc (b); +if (ret) + goto exit_free_reg; + ret =kdbus_ep_new (b,"bus" , mode, uid, gid, false, &b->ep);if (ret <0 ) - goto exit_free_reg; + goto exit_free_security;/* link into domain */ mutex_lock (&domain->lock);
@@ -386,6 +392,8 @@ exit_unref_user_unlock:kdbus_domain_user_unref (b->user);kdbus_ep_disconnect (b->ep);kdbus_ep_unref (b->ep); +exit_free_security: +security_kdbus_bus_free (b); exit_free_reg:kdbus_name_registry_free (b->name_registry); exit_free_name:
drivers/misc/kdbus/connection.c +33 -1
@@ -27,6 +27,7 @@#include <linux/sched.h> #include <linux/shmem_fs.h> #include <linux/sizes.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/syscalls.h>
@@ -282,6 +283,10 @@ int kdbus_cmd_msg_recv(struct kdbus_conn *conn,if (recv->offset >0 )return -EINVAL ; + ret =security_kdbus_recv (conn, conn->ep->bus); +if (ret) +return ret; +mutex_lock (&conn->lock); ret = kdbus_queue_entry_peek(&conn->queue, recv->priority, recv->flags & KDBUS_RECV_USE_PRIORITY,
@@ -460,7 +465,12 @@ static int kdbus_conn_check_access(struct kdbus_ep *ep,if (allowed)return 0 ; -/* ... otherwise, ask the policy DBs for permission */ +/* ... consult LSM */ + ret =security_kdbus_talk (conn_src, conn_dst); +if (ret) +return ret; + +/* ... finally, ask the policy DBs for permission */ ret =kdbus_ep_policy_check_talk_access (ep, conn_src, conn_dst);if (ret <0 )return ret;
@@ -596,6 +606,10 @@ static void kdbus_conn_broadcast(struct kdbus_ep *ep,* data, even when they did not ask for it. */ if (conn_src) { + ret =security_kdbus_talk (conn_src, conn_dst); +if (ret) +continue ; +/* Check if conn_src is allowed to signal */ ret = kdbus_ep_policy_check_broadcast(conn_dst->ep, conn_src,
@@ -736,6 +750,10 @@ int kdbus_conn_kmsg_send(struct kdbus_ep *ep,bool sync = msg->flags &KDBUS_MSG_FLAGS_SYNC_REPLY ;int ret =0 ; + ret =security_kdbus_send (conn_src, bus); +if (ret) +return ret; +/* assign domain-global message sequence number */ BUG_ON (kmsg->seq >0 ); kmsg->seq =atomic64_inc_return (&bus->domain->msg_seq_last);
@@ -1086,6 +1104,7 @@ static void __kdbus_conn_free(struct kref *kref)kdbus_ep_unref (conn->ep);kdbus_bus_unref (conn->bus);put_cred (conn->cred); +security_kdbus_conn_free (conn);kfree (conn->name);kfree (conn); }
@@ -1467,8 +1486,12 @@ int kdbus_conn_new(struct kdbus_ep *ep,bool is_policy_holder;bool is_activator;bool is_monitor; +u32 len; +u32 sid; +char *label;int ret; +BUG_ON (*c); is_monitor = hello->flags &KDBUS_HELLO_MONITOR ;
@@ -1542,6 +1565,9 @@ int kdbus_conn_new(struct kdbus_ep *ep,return -ENOMEM ;if (is_activator || is_policy_holder) { + ret =security_kdbus_ep_setpolicy (bus); +if (ret) + goto exit_free_conn;/* * Policy holders may install one name, and are * allowed to use wildcards.
@@ -1680,6 +1706,12 @@ int kdbus_conn_new(struct kdbus_ep *ep, goto exit_unref_user_unlock; } +security_task_getsecid (current, &sid); +security_secid_to_secctx (sid, &label, &len); + ret =security_kdbus_connect (conn, label, len); +if (ret <0 ) + goto exit_unref_user_unlock; +/* link into bus and endpoint */ list_add_tail (&conn->ep_entry, &ep->conn_list);hash_add (bus->conn_hash, &conn->hentry, conn->id);
drivers/misc/kdbus/domain.c +7 -0
@@ -18,6 +18,7 @@#include <linux/module.h> #include <linux/sched.h> #include <linux/sizes.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/uaccess.h>
@@ -156,6 +157,7 @@ static void __kdbus_domain_free(struct device *dev)idr_destroy (&domain->user_idr);kfree (domain->name);kfree (domain->devpath); +security_kdbus_domain_free (domain);kfree (domain); }
@@ -255,6 +257,10 @@ int kdbus_domain_new(struct kdbus_domain *parent, const char *name,if (ret <0 ) goto exit_put; + ret =security_kdbus_domain_alloc (d); +if (ret) + goto exit_put; +if (parent) {/* lock order: parent domain -> domain */ mutex_lock (&parent->lock);
@@ -444,6 +450,7 @@ static void __kdbus_domain_user_free(struct kref *kref)hash_del (&user->hentry);mutex_unlock (&user->domain->lock); +security_kdbus_domain_free (user->domain);kdbus_domain_unref (user->domain);kfree (user); }
drivers/misc/kdbus/endpoint.c +11 -0
@@ -17,6 +17,7 @@#include <linux/init.h> #include <linux/module.h> #include <linux/sched.h> +#include <linux/security.h> #include <linux/sizes.h> #include <linux/slab.h> #include <linux/uaccess.h>
@@ -197,6 +198,10 @@ int kdbus_ep_new(struct kdbus_bus *bus, const char *name,if (ret <0 ) goto exit_put; + ret =security_kdbus_ep_create (bus); +if (ret) + goto exit_put; +mutex_lock (&bus->lock);if (bus->disconnected) {
@@ -255,6 +260,12 @@ int kdbus_ep_policy_set(struct kdbus_ep *ep,const struct kdbus_item *items, size_t items_size) { + int ret; + + ret =security_kdbus_ep_setpolicy (ep->bus); +if (ret) +return ret; +return kdbus_policy_set (&ep->policy_db, items, items_size,0 , true, ep); }
drivers/misc/kdbus/names.c +9 -0
@@ -22,6 +22,7 @@#include <linux/mutex.h> #include <linux/rwsem.h> #include <linux/sched.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/uaccess.h>
@@ -492,6 +493,10 @@ int kdbus_name_acquire(struct kdbus_name_registry *reg,u32 hash;int ret =0 ; + ret =security_kdbus_name_acquire (conn, name); +if (ret) +return ret; +/* lock order: domain -> bus -> ep -> names -> conn */ mutex_lock (&conn->bus->lock);down_write (®->rwlock);
@@ -876,6 +881,10 @@ int kdbus_cmd_name_list(struct kdbus_name_registry *reg,size_t pos;int ret; + ret =security_kdbus_name_list (conn->bus); +if (ret) +return ret; + policy_db = &conn->ep->policy_db;/* lock order: domain -> bus -> ep -> names -> conn */
Signature · 1 line
2.1.1
PATCH 3/5 kdbus: check if lsm permits installing received fds
In reply to a message not in the archiveFrom: Karol Lewandowski <[email protected]>
--- drivers/misc/kdbus/queue.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/misc/kdbus/queue.c +13 -0
@@ -25,6 +25,7 @@#include <linux/poll.h> #include <linux/sched.h> #include <linux/sizes.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/syscalls.h>
@@ -41,6 +42,18 @@ static int kdbus_queue_entry_fds_install(struct kdbus_queue_entry *entry)int ret, *fds;size_t count; +for (i =0 ; i < entry->fds_count; i++) { + ret =security_file_receive (entry->fds_fp[i]); +if (ret) +return ret; + } + +for (i =0 ; i < entry->memfds_count; i++) { + ret =security_file_receive (entry->memfds_fp[i]); +if (ret) +return ret; + } +/* get array of file descriptors */ count = entry->fds_count + entry->memfds_count;if (!count)
Signature · 1 line
2.1.1
Re: How Not To Use kref (was Re: kdbus: add code for buses, domains and endpoints)
In reply to Al ViroQuoted 5 lines · Al Viro
If you remove an object from some search structures, taking the lock in destructor is Too Fucking Late(tm). Somebody might have already found that puppy and decided to pick it (all under that lock) just as we'd got to that point in destructor and blocked there. Oops...
Ugh, yes. This is a much too common anti-pattern.
Quoted 2 lines
Normally I'd say "just use
kref_put_mutex()", but this case is even worse. Look:
Yeah the whole "release the structure the lock is in" is another one.
Both of these patterns have happened so many times that I'd love to have some kind of automated tool to see them, but I suspect it is *much* too complex to be easily checked for. The lock object debugging we have only triggers for the case where the freeing actually happens with the lock still held, which is too late and too hard-to-hit to be a very useful check.
Linus
Re: How Not To Use kref (was Re: kdbus: add code for buses, domains and endpoints)
Quoted 8 lines · Linus Torvalds
Quoted 5 lines · Al Viro
If you remove an object from some search structures, taking the lock in destructor is Too Fucking Late(tm). Somebody might have already found that puppy and decided to pick it (all under that lock) just as we'd got to that point in destructor and blocked there. Oops...
Ugh, yes. This is a much too common anti-pattern.
*nod*
kref is badly oversold; it's fine for the case when there's no non-counting references to the object, but in this kind of situations the things get subtle. And the main attraction of kref is the promise that it's easy to use and avoids all the subtle issues. It's not specific to kref, of course - the same breakage can and does occur when refcounting is open-coded; for example, we used to have that kind of bug in dput(). What makes it really unpleasant is that easy-to-use-don't-worry-it-takes-care-of-everything assumption...
FWIW, here's another lovely instance (drivers/infiniband/hw/ipath/ipath_mmap.c):
static void ipath_vma_close(struct vm_area_struct *vma)
{
struct ipath_mmap_info *ip = vma->vm_private_data;
kref_put(&ip->ref, ipath_release_mmap_info);
}
void ipath_release_mmap_info(struct kref *ref)
{
struct ipath_mmap_info *ip =
container_of(ref, struct ipath_mmap_info, ref);
struct ipath_ibdev *dev = to_idev(ip->context->device);
spin_lock_irq(&dev->pending_lock);
list_del(&ip->pending_mmaps);
spin_unlock_irq(&dev->pending_lock);
vfree(ip->obj);
kfree(ip);
}
static void ipath_vma_open(struct vm_area_struct *vma)
{
struct ipath_mmap_info *ip = vma->vm_private_data;
kref_get(&ip->ref);
}
int ipath_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
{
...
spin_lock_irq(&dev->pending_lock);
list_for_each_entry_safe(ip, pp, &dev->pending_mmaps,
/* Only the creator is allowed to mmap the object */
if (context != ip->context || (__u64) offset != ip->offset)
continue;
/* Don't allow a mmap larger than the object. */
if (size > ip->size)
break;
list_del_init(&ip->pending_mmaps);
spin_unlock_irq(&dev->pending_lock);
ret = remap_vmalloc_range(vma, ip->obj, 0);
if (ret)
goto done;
vma->vm_ops = &ipath_vm_ops;
vma->vm_private_data = ip;
ipath_vma_open(vma);
goto done;Quoted 8 lines
Quoted 2 lines
Normally I'd say "just use
kref_put_mutex()", but this case is even worse. Look:Yeah the whole "release the structure the lock is in" is another one.
Both of these patterns have happened so many times that I'd love to have some kind of automated tool to see them, but I suspect it is *much* too complex to be easily checked for.
Especially since here the lock is *not* in the object being fed to destructor - it's in the object ours holds a reference to, with destructor dropping that reference and _sometimes_ it ends up being the last one.
As for the first pattern... Frankly, git grep -n -w kref_put, followed by quick look through the destructors for something like list_del catches a _lot_ of that ;-/ Not every match is a bug of that sort, but the fraction of false positives is not too high...
Re: kdbus: add code to gather metadata
In reply to Andy LutomirskiHi Andy,
Quoted 9 lines · Andy Lutomirski
Quoted 2 lines · Daniel Mack
We merely allow user that are connected to a bus to query the credentials of the creator of the bus they're connected to.
Why do you allow this? What purpose does it serve? Is that idea that each application will own one bus? If so, what goes wrong if you only capture the specific credentials that the creator of a given bus asks to have captured?
There are different kinds of buses. There is the system bus and a number of user buses. It's really useful to be able to identify the user who owns one of these user buses, for the sake of access control. More specifically, we have a compatibility service called "bus-proxy" that speaks the old D-Bus socket protocol on one side and translates all messages to kdbus messages onto the other. For that, it needs to enforce the old D-Bus access control semantics, which is described in XML, and has quite elaborate checks. In order to enforce it, it's relevant to be able to compare peer credentials with bus owner credentials, because there's usually a rule that the bus owner UID is allowed more than other peers.
Sure, there are other ways to figure out the identity of the bus, but it's really nice to have similar semantics for identifying the bus owner. kdbus internally has that piece of information anyway, so we decided to export it, optionally. However, that's really a minor detail after all.
Quoted 9 lines
Quoted 4 lines
For example, if a privileged service can reboot the system, it checks whether the asking peer has
CAP_SYS_BOOTset. If it checks for uid==0 instead, and it works in your tests because you happen to test as root, that just a bug in the service, right? But I might have missed your point.The issue is the following: if have the privilege needed to talk to journald, I may want to enhance security by opening a connection to journald (and capture that privilege) and then drop privilege. I should still be able to talk to journald.
Hmm, this is not how D-Bus works, and kdbus stays close the design principles of D-Bus. There's no concept of 'opening connections to services'. You just connect to a bus, and then on that bus, you send individual messages and method calls to other services.
The design of Binder and D-Bus are fundamentally different in that regard. On D-Bus, the focus is really about method call transaction (a method call message and a corresponding reply message), and there's no way to continuously reference a peer via the concept of a 'connection'.
This is why we have this functionality of passing over the caller creds every time a method call is made. The focus is really on the individual method call transaction, each one is individually routed, dispatched and checked for permission. Hence, it should carry individual credential information from the time the call is issued.
So, back to your example: you cannot 'open a connection to journald'. You can only connect to a bus and send messages to journald.
Quoted 5 lines
Alternatively, if the privilege needed to reboot is
CAP_SYS_BOOT, then clients should send that capability bit. Capturing extra information to try to give daemons the flexibility to change their authorization conditions later on just moves the problem if you need to change policy down the line.
This would be similar to changing the Linux kernel so that each system call gets a set of capabilities passed in explicitly. RPC calls are very much comparable to syscalls, though they don't transition into kernel space, but simply into another process.
We've been augmenting what syscalls check on for access control ever since. Initially, it check was UID based, then became capability based, and nowadays we have a concept of MACs, that are actually different on every system, because some systems use SElinux, some SMACK or some other MAC. This is why this metadata should be implicit and controlled by the receiver, not by the sender, because the implementation of the policy might change eventually, be extended with more sophisticated access control etc.
But it's not only about access control, there's also auditing. In order to generate useful audit logs, a system service that is offering privileged operation to certain clients needs to know the audit credentials (sessionid and loginuid). Hence, this information needs to be implicitly appended, controlled by that service. Because if it is not implicitly appended, than it will more often be missing than expected (simply because in real-life very few people actually use auditing), and the system service would not be able to log about it.
So, this is not a metadata leak by accident but metadata that system services need to know about in order to work properly. Individual services will require slightly different components of these credentials, but if you combine things, they need to know pretty much all of the details we currently offer as implied metadata.
The system bus is about unprivileged apps asking system services for system operations, in which case the system services must have a way to know who wants them to do what. The purpose of a system bus is _not_ to allow unprivileged peers to talk to each other, that's actually even forbidden in the default policy. That's what user buses are for, and on those, pretty much every client will have the same privileges anyway, hence there's no information leak there either.
Quoted 6 lines
What's the actual problem for logging? I very much understand why a logging service never wants to log an incorrect credential (and legacy syslog has serious problems here because it doesn't even try to capture credential), but what's wrong with having a log that shows the uid for legit log messages and that reliably says "declined to state" for messages that decline to state.
A system's administrator should be able to gather all sorts of information about things that happened on a system. Trying to hide associated metadata is not how things are done anyway - we show it in numerous places, in /proc, in SCM_CREDENTIALS, by listing /tmp or /home etc.
The separation to limit what is passed around is, in our concept, rather on the level of connecting to separate buses, PID namespaces, kdbus domains etc, than to suppress information.
Quoted 2 lines
(Also, I presume that cmdline is for logging. Keep in mind that the cmdline is yanked from user memory and can be freely spoofed.)
Sure. But If a task did that, we want to know about it, and log it accordingly. Journald provides such features already today, and it's a great deal in detecting runtime inconsistencies between a task's real nature and what is displayed in 'ps'.
Thanks,
Daniel
Re: kdbus: add code to gather metadata
Quoted 25 lines · Daniel Mack
Hi Andy,
Quoted 9 lines · Andy Lutomirski
Quoted 2 lines · Daniel Mack
We merely allow user that are connected to a bus to query the credentials of the creator of the bus they're connected to.
Why do you allow this? What purpose does it serve? Is that idea that each application will own one bus? If so, what goes wrong if you only capture the specific credentials that the creator of a given bus asks to have captured?
There are different kinds of buses. There is the system bus and a number of user buses. It's really useful to be able to identify the user who owns one of these user buses, for the sake of access control. More specifically, we have a compatibility service called "bus-proxy" that speaks the old D-Bus socket protocol on one side and translates all messages to kdbus messages onto the other. For that, it needs to enforce the old D-Bus access control semantics, which is described in XML, and has quite elaborate checks. In order to enforce it, it's relevant to be able to compare peer credentials with bus owner credentials, because there's usually a rule that the bus owner UID is allowed more than other peers.
Quoted 6 lines
Sure, there are other ways to figure out the identity of the bus, but it's really nice to have similar semantics for identifying the bus owner. kdbus internally has that piece of information anyway, so we decided to export it, optionally. However, that's really a minor detail after all.
I'm sceptical about the kernel offering APIs just because it can. I'm not fundamentally opposed to objects (e.g. busses) having ownership information, but I think it needs to be well-justified.
Keep in mind that the kernel *has* a concept of ownership. It's uid, gid, and security label. Having the creator's full set of caps and even *command line* as part of the ownership information is really weird.
Quoted 20 lines
Quoted 9 lines
Quoted 4 lines
For example, if a privileged service can reboot the system, it checks whether the asking peer has
CAP_SYS_BOOTset. If it checks for uid==0 instead, and it works in your tests because you happen to test as root, that just a bug in the service, right? But I might have missed your point.The issue is the following: if have the privilege needed to talk to journald, I may want to enhance security by opening a connection to journald (and capture that privilege) and then drop privilege. I should still be able to talk to journald.
Hmm, this is not how D-Bus works, and kdbus stays close the design principles of D-Bus. There's no concept of 'opening connections to services'. You just connect to a bus, and then on that bus, you send individual messages and method calls to other services.
The design of Binder and D-Bus are fundamentally different in that regard. On D-Bus, the focus is really about method call transaction (a method call message and a corresponding reply message), and there's no way to continuously reference a peer via the concept of a 'connection'.
Quoted 8 lines
This is why we have this functionality of passing over the caller creds every time a method call is made. The focus is really on the individual method call transaction, each one is individually routed, dispatched and checked for permission. Hence, it should carry individual credential information from the time the call is issued.
So, back to your example: you cannot 'open a connection to journald'. You can only connect to a bus and send messages to journald.
But you can open a kdbus fd. IMO there should be some actual thought as to what should happen if that credentials change after the fd is opened. The default answer in UNIX is that credentials are only checked at open time. Violating that should have a good reason.
Quoted 11 lines
Quoted 6 lines
Alternatively, if the privilege needed to reboot is
CAP_SYS_BOOT, then clients should send that capability bit. Capturing extra information to try to give daemons the flexibility to change their authorization conditions later on just moves the problem if you need to change policy down the line.This would be similar to changing the Linux kernel so that each system call gets a set of capabilities passed in explicitly. RPC calls are very much comparable to syscalls, though they don't transition into kernel space, but simply into another process.
That would be a fantastic idea, and, in fact, it would have been vastly better if syscalls had always worked that way. It's how anything on a network *has* to work, it's how object capability systems work (and object capability systems are much less prone to security bugs caused by overcomplicated implicit checks), and it's how Capsicum (which builds on POSIX!) works.
Quoted 9 lines
We've been augmenting what syscalls check on for access control ever since. Initially, it check was UID based, then became capability based, and nowadays we have a concept of MACs, that are actually different on every system, because some systems use SElinux, some SMACK or some other MAC. This is why this metadata should be implicit and controlled by the receiver, not by the sender, because the implementation of the policy might change eventually, be extended with more sophisticated access control etc.
I agree that this is convenient, but it's a hack, and we've only gotten away with it so far because only something that's very much in the trusted computing base can see this information at all. And I strongly suspect that, if I were inclined to try to break SELinux, I could find any number of rather fundamental holes based on the fact that it adds checks to capabilities in places where they didn't exist originally.
Quoted 10 lines
But it's not only about access control, there's also auditing. In order to generate useful audit logs, a system service that is offering privileged operation to certain clients needs to know the audit credentials (sessionid and loginuid). Hence, this information needs to be implicitly appended, controlled by that service. Because if it is not implicitly appended, than it will more often be missing than expected (simply because in real-life very few people actually use auditing), and the system service would not be able to log about it.
Quoted 5 lines
So, this is not a metadata leak by accident but metadata that system services need to know about in order to work properly. Individual services will require slightly different components of these credentials, but if you combine things, they need to know pretty much all of the details we currently offer as implied metadata.
Sorry, but this is bogus. The audit system is a bit of an unstable mess, and, to all appearances, its main design goal seems to be regulatory compliance instead of actual security. It seems to be mostly harmless, but only because all of the data gathered by auditd that should never have existed in the first place can only be collected by an extremely privileged global daemon and is shoved in logs that no one outside the TCB can read.
If you want a configurable-out off-by-default bolt-on system that allows auditd (and nothing else!) to sniff kdbus busses and log whatever random crap it wants to log (and keep it the hell out of the part of the journal that unprivileged users can see), then do so.
Quoted 8 lines
The system bus is about unprivileged apps asking system services for system operations, in which case the system services must have a way to know who wants them to do what. The purpose of a system bus is _not_ to allow unprivileged peers to talk to each other, that's actually even forbidden in the default policy. That's what user buses are for, and on those, pretty much every client will have the same privileges anyway, hence there's no information leak there either.
This is nonsense. If you don't need real access control on user busses, then *don't add any*.
Except that you *do* need real access control, because of containers, seccomp, the Chromium sandbox, the upcoming Firefox sandbox, all of the other little sandboxes that should exist but don't yet, remoting, etc. And I guarantee that your list of implicitly transmitted credentials will fail to handle this case, and this case is the only one that really matters. At the end of the day, you're going to need either crypto, something like object capabilities, or an access broker. And none of those benefit at all from the current proposed model. (And none of them check anything like implicit credentials at the time of an RPC call, either.)
Interestingly, it sounds to be like traditional D-Bus will work considerably better than kdbus in this scenario.
As a concrete example, Wayland takes sensitive operations like screenshots very seriously. Which of the kdbus metadata items would be appropriate to use for access control for something like that?
Quoted 8 lines
Quoted 7 lines
What's the actual problem for logging? I very much understand why a logging service never wants to log an incorrect credential (and legacy syslog has serious problems here because it doesn't even try to capture credential), but what's wrong with having a log that shows the uid for legit log messages and that reliably says "declined to state" for messages that decline to state.
Quoted 4 lines
A system's administrator should be able to gather all sorts of information about things that happened on a system. Trying to hide associated metadata is not how things are done anyway - we show it in numerous places, in /proc, in
SCM_CREDENTIALS, by listing /tmp or /home etc.
See above. The sysadmin != every single kdbus user.
Quoted 8 lines
Quoted 2 lines
(Also, I presume that cmdline is for logging. Keep in mind that the cmdline is yanked from user memory and can be freely spoofed.)
Sure. But If a task did that, we want to know about it, and log it accordingly. Journald provides such features already today, and it's a great deal in detecting runtime inconsistencies between a task's real nature and what is displayed in 'ps'.
Sorry, but this is a bit off the deep end. The kdbus logging mechanism shouldn't be in the business of trying to do this, because it's out of scope, and it will fail.
You can't justify logging fundamentally unverifiable things like the command line by saying that you want to know if someone tries to play (impossible-to-reliably-detect) games to obscure their command line.
--Andy
Greg Kroah-Hartman
maintainerQuoted 6 lines · Jiri Kosina
Quoted 3 lines · Greg Kroah-Hartman
kdbus is a kernel-level IPC implementation that aims for resemblance to the the protocol layer with the existing userspace D-Bus daemon while enabling some features that couldn't be implemented before in userspace.
Quoted 8 lines
I'd be interested in the features that can't be implemented in userspace (and therefore would justify existence of kdbus in the kernel). Could you please point me to such list / documentation?
It seems to me that most of the highlight features from the cover letter can be "easily" (for certain definition of that word, of course) implemented in userspace (
vmsplice(), sending fd through unix socket, user namespaces, UUID management, etc).
Sorry for the long delay in getting back to this, I'm battling a bad case of jet-lag at the moment...
Here's some reasons why I feel it is better to have kdbus in the kernel rather than trying to implement the same thing in a userspace daemon:
- performance: fewer process context switches, fewer copies, fewer syscalls, larger memory chunks via memfd. This is really important for a whole class of userspace programs that are ported from other operating systems that are run on tiny ARM systems that rely on hundreds of thousands of messages passed at boot time, and at "critical" times in their user interaction loops.
- security: the peers which communicate do not have to trust each other, as the only trustworthy compoenent in the game is the kernel which adds metadata and ensures that all data passed as payload is either copied or sealed, so that the receiver can parse the data without having to protect against changing memory while parsing buffers. Also, all the data transfer is controlled by the kernel, so that LSMs can track and control what is going on, without involving userspace. Because of the LSM issue, security people are much happier with this model than the current scheme of having to hook into dbus to mediate things.
- more metadata can be attached to messages than in userspace
- semantics for apps with heavy data payloads (media apps, for instance) with optinal priority message dequeuing, and global message ordering. Some "crazy" people are playing with using kdbus for audio data in the system. I'm not saying that this is the best model for this, but until now, there wasn't any other way to do this without having to create custom "busses", one for each application library.
- being in the kernle closes a lot of races which can't be fixed with the current userspace solutions. For example, with kdbus, there is a way a client can disconnect from a bus, but do so only if no further messages present in its queue, which is crucial for implementing race-free "exit-on-idle" services
- eavesdropping on the kernel level, so privileged users can hook into the message stream without hacking support for that into their userspace processes
- a number of smaller benefits: for example kdbus learned a way to peek full messages without dequeing them, which is really useful for logging metadata when handling bus-activation requests.
Of course, some of the bits above could be implemented in userspace alone, for example with more sophisticated memory management APIs, but this is usually done by losing out on the other details. For example, for many of the memory management APIs, it's hard to not require the communicating peers to fully trust each other. And we _really_ don't want peers to have to trust each other.
Another benefit of having this in the kernel, rather than as a userspace daemon, is that you can now easily use the bus from the initrd, or up to the very end when the system shuts down. On current userspace D-Bus, this is not really possible, as this requires passing the bus instance around between initrd and the "real" system. Such a transition of all fds also requires keeping full state of what has already been read from the connection fds. kdbus makes this much simpler, as we can change the ownership of the bus, just by passing one fd over from one part to the other.
Regarding binder: binder and kdbus follow very different design concepts. Binder implies the use of thread-pools to dispatch incoming method calls. This is a very efficient scheme, and completely natural in programming languages like Java. On most Linux programs, however, there's a much stronger focus on central poll() loops that dispatch all sources a program cares about. kdbus is much more usable in such environments, as it doesn't enforce a threading model, and it is happy with serialized dispatching. In fact, this major difference had an effect on much of the design decisions: binder does not guarantee global message ordering due to the parallel dispatching in the thread-pools, but kdbus does. Moreover, there's also a difference in the way message handling. In kdbus, every message is basically taken and dispatched as one blob, while in binder, continious connections to other peers are created, which are then used to send messages on. Hence, the models are quite different, and they serve different needs. I believe that the D-Bus/kdbus model is more compatible and friendly with how Linux programs are usually implemented. I went into the kdbus vs. binder stuff in a blog post that I linked to earlier in this thread that goes into more detail here.
Hopefully this helps explain why I feel kdbus should be in the kernel and not a userspace daemon. I'll put this information in the cover letter for the next round of patches that are sent out.
thanks,
greg k-h
Greg Kroah-Hartman
maintainerRe: kdbus: add documentation
In reply to Peter MeerwaldQuoted 9 lines · Peter Meerwald
Quoted 7 lines
kdbus is a system for low-latency, low-overhead, easy to use interprocess communication (IPC).
The interface to all functions in this driver is implemented through ioctls on /dev nodes. This patch adds detailed documentation about the kernel level API design.
just some typos below
<snip>
Many thanks for the fixes, I've made them all to the file now, it will show up in the next version we send out.
greg k-h
Re: kdbus: add code to gather metadata
In reply to Andy LutomirskiQuoted 3 lines · Andy Lutomirski
You can't justify logging fundamentally unverifiable things like the command line by saying that you want to know if someone tries to play (impossible-to-reliably-detect) games to obscure their command line.
I think kdbus might be mixing up two orthogonal things here.
It has an easy, kernel-checked, race-free way to determine kernel-mediated credential-like information that cannot be faked or interfered with (uid, primary gid, other gids?, security label, capabilities) because these are usable for security decisions, but if they are *not* received in a kernel-checked, race-free way, then they are useless.
One concrete example of using non-ucred credential-like information is that traditional D-Bus can only restrict sysadmin tasks to uid 0 (or a root-equivalent uid in group sudo/admin/whatever), whereas when systemd and systemd-logind are run on kdbus, many of their D-Bus methods require specific capabilities(7): KillUser requires CAP_KILL, PowerOff requires CAP_SYS_BOOT, and so on. If capabilities(7) are a good thing, then that's surely a good thing too. (On the other hand, if you think capabilities(7) are a waste of time, then so is this.)
It also uses the same mechanism as an easy, race-free, but *not* kernel-checked way to determine bits and pieces that are valuable for debugging (dbus-monitor etc.), but unsuitable for security decisions, such as cmdline.
In traditional D-Bus, you can get the uid and pid of a remote process, but in a debug log you would probably actually prefer to log the cmdline in addition; yes a malicious user could fake the cmdline, but when debugging a system problem, information that is known to be forgeable seems better than no information at all. After all, ps(1) shows the forgeable cmdline, not just the executable. You can get that by rummaging in /proc/$pid, but there is a race: if the remote process exits too soon (a "fire and forget" method call) then you'll never know who it was. kdbus solves that race, but does not make cmdline unforgeable.
If client libraries wishing to attach their cmdline (or other debug info) to messages for debugging were required to add it as an out-of-band KDBUS_ITEM, or as a D-Bus message header inside the payload, then that would be duplicating work in client libraries that could have been done centrally, but would still solve the race.
S
Quoted 2 lines · On Sat, 1 Nov 2014 18:21:30 -0700 Greg Kroah-Hartman…
Here's some reasons why I feel it is better to have kdbus in the kernel rather than trying to implement the same thing in a userspace daemon:
No - these are reasons to have *something* in the kernel. I think it would be far more constructive to treat the current kdbus as a proof of concept/prototype or even a draft requirements specification.
Quoted 3 lines
as the only trustworthy compoenent in the game is the kernel which adds metadata and ensures that all data passed as payload is either copied or sealed, so that the receiver can parse the data without
When the kernel adds metadata without being told to do so by one end of the link you create a new set of security and privacy leaks. Far better that the sender must choose what metadata is added and the receiver can decide to bin stuff that's not acceptable. The job of the kernel is really more like that of an auditor in a business transaction - to make sure that the data they agree to pass is truthful.
(ie its the sender who must say "attach my user info", the receiver who must say "no info, no play" and the kernel who must provide the info so it can't be faked.
Quoted 2 lines
- semantics for apps with heavy data payloads (media apps, for instance) with optinal priority message dequeuing, and global message ordering.
Sounds like System 5 IPC ;-)
Quoted 2 lines
Regarding binder: binder and kdbus follow very different design concepts.
We know binder is broken but the Android guys are stuck in a special kind of hell with it for some years to come. We need to make sure kdbus isn't the same result.
Alan
Re: kdbus: add code to gather metadata
In reply to Simon McVittieQuoted 14 lines · Simon McVittie
Quoted 3 lines · Andy Lutomirski
You can't justify logging fundamentally unverifiable things like the command line by saying that you want to know if someone tries to play (impossible-to-reliably-detect) games to obscure their command line.
I think kdbus might be mixing up two orthogonal things here.
It has an easy, kernel-checked, race-free way to determine kernel-mediated credential-like information that cannot be faked or interfered with (uid, primary gid, other gids?, security label, capabilities) because these are usable for security decisions, but if they are *not* received in a kernel-checked, race-free way, then they are useless.
Quoted 8 lines
One concrete example of using non-ucred credential-like information is that traditional D-Bus can only restrict sysadmin tasks to uid 0 (or a root-equivalent uid in group sudo/admin/whatever), whereas when systemd and systemd-logind are run on kdbus, many of their D-Bus methods require specific capabilities(7): KillUser requires
CAP_KILL, PowerOff requiresCAP_SYS_BOOT, and so on. If capabilities(7) are a good thing, then that's surely a good thing too. (On the other hand, if you think capabilities(7) are a waste of time, then so is this.)
I think that capabilities(7) is largely a disaster. That aside, I don't think that all of these capabilities should refer to the *kernel* privileges. For example, CAP_SYS_BOOT should be, and remain, the ability to reboot the system *yourself*. For example, if someone wants to implement Windows 7 (or Visa? Or 2003? I forget.) style reboot auditing, then userspace specifically does not want programs that can ask for a reboot to hold CAP_SYS_BOOT.
Quoted 6 lines
It also uses the same mechanism as an easy, race-free, but *not* kernel-checked way to determine bits and pieces that are valuable for debugging (dbus-monitor etc.), but unsuitable for security decisions, such as cmdline.
Quoted 10 lines
In traditional D-Bus, you can get the uid and pid of a remote process, but in a debug log you would probably actually prefer to log the cmdline in addition; yes a malicious user could fake the cmdline, but when debugging a system problem, information that is known to be forgeable seems better than no information at all. After all, ps(1) shows the forgeable cmdline, not just the executable. You can get that by rummaging in
/proc/$pid, but there is a race: if the remote process exits too soon (a "fire and forget" method call) then you'll never know who it was. kdbus solves that race, but does not make cmdline unforgeable.
I would love to fix this race. That opens the door to a lot more debugging (maps, status, etc) while not pretending to offer a kernel check that doesn't, and can't, exist.
Materializing some sort of pid fd on the receiving end would be one solution. Another would be to give the receiver some token that can be used to check for pid recycling.
Can we give each task a pid-namespace-local 64-bit (or even longer) number that is mostly guaranteed not to be reused for the lifetime of the namespace?
For example, give each task a per-pidns tid_unique, and give each pidns a next_tid_unique. Creating a task assigns it next_tid_unique++. (On overflow, clone fails.) For CRIU's benefit, if you have CAP_SYS_ADMIN over the pidns's userns and you are not in the pidns, then you can change tid_unique and next_tid_unique. Doing that carelessly will introduce races, but that's fine -- it should only be done on restore when everything's frozen.
Quoted 6 lines
If client libraries wishing to attach their cmdline (or other debug info) to messages for debugging were required to add it as an out-of-band
KDBUS_ITEM, or as a D-Bus message header inside the payload, then that would be duplicating work in client libraries that could have been done centrally, but would still solve the race.
I much prefer that approach. If a kernel feature is being added to just to avoid duplication of a debugging aid in user code, then let's leave it to user code.
--Andy
Re: How Not To Use kref (was Re: kdbus: add code for buses, domains and endpoints)
In reply to Al ViroHi Al
Quoted 21 lines · Al Viro
drivers/misc/kdbus/domain.c line 442
Quoted 9 lines · Greg Kroah-Hartman
drivers/misc/kdbus/domain.c new file +9
-0+static void __kdbus_domain_user_free (struct kref *kref) +{ +struct kdbus_domain_user *user = + container_of(kref, struct kdbus_domain_user, kref); + +BUG_ON (atomic_read (&user->buses) >0 ); +BUG_ON (atomic_read (&user->connections) >0 ); + +mutex_lock (&user->domain->lock);^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^drivers/misc/kdbus/domain.c line 444
Quoted 2 lines
drivers/misc/kdbus/domain.c new file +2
-0+idr_remove (&user->domain->user_idr, user->idr); +hash_del (&user->hentry);^^^^^^^^^^^^^^^^^^^^^^^^Quoted 5 lines
drivers/misc/kdbus/domain.c new file +5
-0+mutex_unlock (&user->domain->lock); + +kdbus_domain_unref (user->domain); +kfree (user); +}
Quoted 11 lines
drivers/misc/kdbus/domain.c line 477
Quoted 6 lines
drivers/misc/kdbus/domain.c new file +6
-0+struct kdbus_domain_user *kdbus_domain_user_unref (struct kdbus_domain_user *u) +{ +if (u) +kref_put (&u->kref, __kdbus_domain_user_free); +return NULL ; +}If you remove an object from some search structures, taking the lock in destructor is Too Fucking Late(tm). Somebody might have already found that puppy and decided to pick it (all under that lock) just as we'd got to that point in destructor and blocked there. Oops...
Nice catch! I fixed it up via kref_get_unless_zero(). This has the side-effect that there might be multiple domain_user objects for the same user, but all but one will have ref==0. They don't carry and valuable data in those cases, so we're fine. We will just end up using the next one, or creating a new one.
Thanks a lot!
David
Quoted 5 lines · Andy Lutomirski
I think that each piece of trustable metadata needs to be explicitly opted-in to by the sender at the time of capture. Otherwise you're asking for lots of information leaks and privilege escalations. This is especially important given that some of the items in the current list could be rather sensitive.
Alright, the above seems to pretty much sum up that end of our discussion. To address this, We've now added the following functionality for v2:
* The attach_flags in kdbus_cmd_hello was split into two parts, attach_flags_send and attach_flags_recv, so each peer may chose what exactly it want to transmit or receive.
* Metadata will only be attached to the final message in the receiver's pool if both the sender's attach_flags_send and the receiver's attach_flags_recv bit are set.
* Consequently, the existing KDBUS_ITEM_ATTACH_FLAGS item type is split into KDBUS_ITEM_ATTACH_FLAGS_SEND and KDBUS_ITEM_ATTACH_FLAGS_RECV, so that both connection details can be separately updated through KDBUS_CMD_CONN_UPDATE.
* To allow for use cases that require certain metadata to be attached on each message, we've added a negotiation mechanism to the HELLO
ioctl: An optional metadata mask can be passed during the creation of buses, so bus owners may require certain bits in attach_flags_send to be set. That way, the creator of the bus will specify which metadata is required to fulfill the requirements of the specification of the role of the bus.
Thanks again for your input!
Daniel
Re: kdbus: add code for buses, domains and endpoints
In reply to Simon McVittieQuoted 6 lines · Simon McVittie
Quoted 4 lines · Djalal Harouni
So, this is similar to
AF_UNIXsockets. For them there'sSCM_CREDENTIALSandSO_PEERCRED. The former uses credentials at the time of when messages are being sent, the latter uses the credentials at the time when when the connection was initially established.
Quoted 9 lines
Please note that dbus-daemon, the reference implementation of D-Bus, does not actually ever use
SCM_CREDENTIALSon itsAF_UNIXsockets. We prefer to use Linux'sSO_PEERCRED, or the platform's closest available equivalent if there is one. dbus-daemon has methods (RPC calls) to get a specified peer's uid, pid or LSM data (e.g. SELinux context), but those methods return the value that was true when the connection was opened or shortly afterwards, not the value that is true right now. I believe the plan is that kdbus has ioctls that are equivalent to those RPC calls, but without needing to wait for asynchronous socket events to get an answer.
Correct, we are compatible to SO_PEERCRED and every peer can request that using KDBUS_CMD_CONN_INFO ioctl(), no need for asynchronous operations.
Thank you Simon for your feedback!
Signature · 2 lines
Djalal Harouni http://opendz.org
Greg KH
maintainerRe: [RFC PATCH 0/5] kdbus: add support for lsm
In reply to Karol LewandowskiQuoted 28 lines · Karol Lewandowski
This is set of EXPERIMENTAL patches adding lsm support to kdbus. (Rebased on top of v3.17.)
Quoted 1 line
From least to most invasive:
- (1) kdbus: extend structures with security pointer for lsm
Trivial. Applicable as-is.
- (2) security: export
security_file_receivefor modules
(3) kdbus: check if lsm permits installing received fds
fd_installdoesn't seem to consult LSM, these patches ensure that receiving process has the right to sent fds.Compile-tested only.
- (4) security: introduce lsm hooks for kdbus
(5) kdbus: make use of new lsm hooksSet of proof-of-concept hooks discussed previously with Paul Moore.
kdbus integration patch (5) for review, but unlikely for integration at this stage.
Likewise, compile-tested only.
Quoted 6 lines
Karol Lewandowski (5):
kdbus: extend structures with security pointer for lsm
security: exportsecurity_file_receivefor modules
kdbus: check if lsm permits installing received fds
security: introduce lsm hooks for kdbus
kdbus: make use of new lsm hooks
These looks reasonable to me, thanks for sending them. They will need to be refreshed again after this next round of changes, but it shouldn't be that hard to do so.
thanks,
greg k-h
Re: [RFC PATCH 0/5] kdbus: add support for lsm
Quoted 36 lines · Greg KH
Quoted 28 lines · Karol Lewandowski
This is set of EXPERIMENTAL patches adding lsm support to kdbus. (Rebased on top of v3.17.)
Quoted 1 line
From least to most invasive:
- (1) kdbus: extend structures with security pointer for lsm
Trivial. Applicable as-is.
- (2) security: export
security_file_receivefor modules
(3) kdbus: check if lsm permits installing received fds
fd_installdoesn't seem to consult LSM, these patches ensure that receiving process has the right to sent fds.Compile-tested only.
- (4) security: introduce lsm hooks for kdbus
(5) kdbus: make use of new lsm hooksSet of proof-of-concept hooks discussed previously with Paul Moore.
kdbus integration patch (5) for review, but unlikely for integration at this stage.
Likewise, compile-tested only.
Quoted 6 lines
Karol Lewandowski (5):
kdbus: extend structures with security pointer for lsm
security: exportsecurity_file_receivefor modules
kdbus: check if lsm permits installing received fds
security: introduce lsm hooks for kdbus
kdbus: make use of new lsm hooks
Quoted 3 lines
These looks reasonable to me, thanks for sending them. They will need to be refreshed again after this next round of changes, but it shouldn't be that hard to do so.
Sure thing.
For completness - there are accompanying Smack and SELinux patches that could go together with above patches, ie.
https://github.com/lmctl/linux/commit/103c26fd27d1ec8c32d85dd3d85681f936ac66fb
http://git.infradead.org/users/pcmoore/selinux/commitdiff/eef4844f91fef6092b6bfac941ebe7f18375be9d
I've got some free time on my hands now, so I'll try to revisit these too.
Cheers,
Karol Lewandowski
Re: kdbus: add selftests
In reply to Greg Kroah-HartmanQuoted 8 lines · Greg Kroah-Hartman
From: Daniel Mack <[email protected]>
This patch adds a quite extensive test suite for kdbus that checks the most important code pathes in the driver. The idea is to extend the test suite over time.
Also, this code can serve as an example implementation to show how to use the kernel API from userspace.
Great to see selftests included.
I needed this to get them building:
include/uapi/linux/Kbuild +1 -0
@@ -210,6 +210,7 @@ header-y += ixjuser.h header-y += jffs2.h header-y += joystick.h header-y += kd.h +header-y += kdbus.h header-y += kdev_t.h header-y += kernel-page-flags.h header-y += kernel.h
tools/testing/selftests/kdbus/Makefile +2 -2
@@ -2,7 +2,7 @@ CFLAGS += -I../../../../usr/include/ CFLAGS += -I../../../../include/uapi/ CFLAGS += -std=gnu99 CFLAGS += -DKBUILD_MODNAME=\"kdbus\" -D_GNU_SOURCE -LDFLAGS = -pthread -lcap +LDLIBS = -pthread -lcap OBJS= \ kdbus-enum.o \
@@ -37,7 +37,7 @@ all: kdbus-test gcc $(CFLAGS) -c $< -o $@ kdbus-test: $(OBJS) - gcc $(CFLAGS) $(LDFLAGS) $^ -o $@ + gcc $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ run_tests: ./kdbus-test
And with that it's all happy on ppc64le:
Testing bus make functions (bus-make) .................................. OK Testing the HELLO command (hello) ...................................... OK Testing the BYEBYE command (byebye) .................................... OK Testing a chat pattern (chat) .......................................... OK Testing a simple dameon (daemon) ....................................... OK Testing file descriptor passing (fd-passing) ........................... OK Testing custom endpoint (endpoint) ..................................... OK Testing monitor functionality (monitor) ................................ OK Testing basic name registry functions (name-basics) .................... OK Testing name registry conflict details (name-conflict) ................. OK Testing queuing of names (name-queue) .................................. OK Testing basic message handling (message-basic) ......................... OK Testing handling of messages with priority (message-prio) .............. OK Testing timeout (timeout) .............................................. OK Testing synchronous replies vs. BYEBYE (sync-byebye) ................... OK Testing synchronous replies (sync-reply) ............................... OK Testing freeing of memory (message-free) ............................... OK Testing retrieving connection information (connection-info) ............ OK Testing updating connection information (connection-update) ............ OK Testing verifying pools are never writable (writable-pool) ............. OK Testing policy (policy) ................................................ OK Testing unprivileged bus access (policy-priv) .......................... OK Testing policy in user namespaces (policy-ns) .......................... OK Testing metadata in user namespaces (metadata-ns) ...................... OK Testing adding of matches by id (match-id-add) ......................... OK Testing removing of matches by id (match-id-remove) .................... OK Testing adding of matches by name (match-name-add) ..................... OK Testing removing of matches by name (match-name-remove) ................ OK Testing matching for name changes (match-name-change) .................. OK Testing matching with bloom filters (match-bloom) ...................... OK Testing activator connections (activator) .............................. OK Testing creating a domain (domain-make) ................................ OK Testing benchmark (benchmark) .......................................... OK Testing race multiple byebyes (race-byebye) ............................ OK Testing race byebye vs match removal (race-byebye-match) ............... OK
SUMMARY: 35 tests passed, 0 skipped, 0 failed
cheers
Re: kdbus: add selftests
Quoted 7 lines · Michael Ellerman
Quoted 6 lines · Greg Kroah-Hartman
From: Daniel Mack <[email protected]>
This patch adds a quite extensive test suite for kdbus that checks the most important code pathes in the driver. The idea is to extend the test suite over time.
Quoted 6 lines
Quoted 2 lines
Also, this code can serve as an example implementation to show how to use the kernel API from userspace.
Great to see selftests included.
I needed this to get them building:
Thanks a lot for testing! I've added your hunks to the patch set now.
Daniel
Re: [PATCH 1/5] kdbus: extend structures with security pointer for lsm
In reply to Karol LewandowskiQuoted 20 lines · Karol Lewandowski
Signed-off-by Karol Lewandowski <[email protected]>--- drivers/misc/kdbus/bus.h | 2 ++ drivers/misc/kdbus/connection.h | 2 ++ drivers/misc/kdbus/domain.h | 2 ++ 3 files changed, 6 insertions(+)drivers/misc/kdbus/bus.h +1
-0@@ -49,6 +49,7 @@* @conn_hash: Map of connection IDs * @monitors_list: Connections that monitor this bus * @meta: Meta information about the bus creator +* @security: LSM security blob * * A bus provides a "bus" endpoint / device node. *
drivers/misc/kdbus/bus.h struct kdbus_bus { · line 88
Quoted 4 lines
drivers/misc/kdbus/bus.h +1
-0@@ -84,6 +85,7 @@ struct kdbus_bus {struct list_head monitors_list;struct kdbus_meta *meta; +void *security;
One minor note - with possibility of LSM modules trying to access these structures it would be worth to have kdbus headers in, say include/linux/kdbus or similar globally accessible location.
Thanks
Greg KH
maintainerRe: [PATCH 1/5] kdbus: extend structures with security pointer for lsm
Quoted 23 lines · Karol Lewandowski
Quoted 22 lines · Karol Lewandowski
Signed-off-by Karol Lewandowski <[email protected]>--- drivers/misc/kdbus/bus.h | 2 ++ drivers/misc/kdbus/connection.h | 2 ++ drivers/misc/kdbus/domain.h | 2 ++ 3 files changed, 6 insertions(+)drivers/misc/kdbus/bus.h +1
-0@@ -49,6 +49,7 @@* @conn_hash: Map of connection IDs * @monitors_list: Connections that monitor this bus * @meta: Meta information about the bus creator +* @security: LSM security blob * * A bus provides a "bus" endpoint / device node. * @@ -84,6 +85,7 @@ struct kdbus_bus {struct list_head monitors_list;
Quoted 6 lines
drivers/misc/kdbus/bus.h struct kdbus_bus { · line 88
Quoted 2 lines
drivers/misc/kdbus/bus.h +1
-0struct kdbus_meta *meta; +void *security;One minor note - with possibility of LSM modules trying to access these structures it would be worth to have kdbus headers in, say
include/linux/kdbusor similar globally accessible location.
Yes, that is a good idea.