rules: set group ownership of new firewire driver device
PATCH 1/1 rules: set group ownership of new firewire driver device
The newer firewire-core driver exposes per-device character device files, called /dev/fw*, in contrast to the older raw1394, video1394, dv1394 drivers which created one global /dev/raw1394 and per controller/ per format files.
This allows to set ownership, permissions, or/ and access control lists for each device file based on device type markers obtained from sysfs. For example, access to the device file of a video camera could be allowed to users who belong to a "video" group, while files of other device types (storage devices, local node...) remain accessible only to root by default. This is what this change implements.
[Another approach has been implemented by Fedora's rules, an additional script, and presumably PolicyKit: The script creates symlinks whose names reflect the device type, and then the current console owner is added to the ACLs of the device files based on name patterns of the symlinks.]
There is a catch though: The device capabilities are shown in child devices of the actual devices which provide the character device files. Hence the charcter device files already exist when the "add" events happen from which we get the device type (the so-called unit architecture). Therefore the 'PROGRAM=/bin/chgrp...' directive is used instead of a simple 'GROUP=...' directive.
--- rules/rules.d/50-udev-default.rules | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
rules/rules.d/50-udev-default.rules +7 -1
@@ -43,10 +43,16 @@ SUBSYSTEM="drm", GROUP="video" # DVB (video) SUBSYSTEM="dvb", ENV{DVB_ADAPTER_NUM}="?*", NAME="dvb/adapter$env{DVB_ADAPTER_NUM}/$env{DVB_DEVICE_TYPE}$env{DVB_DEVICE_NUM}", GROUP="video" -# Firewire +# Firewire, old driver stack KERNEL="dv1394-[0-9]*", NAME="dv1394/%n", GROUP="video" KERNEL="video1394-[0-9]*", NAME="video1394/%n", GROUP="video" +# Firewire, new driver stack +# IIDC devices: industrial cameras and some webcams +SUBSYSTEM="firewire", ATTR{specifier_id}="0x00a02d", ATTR{version}="0x00010?", PROGRAM="/bin/chgrp video /dev/%P" +# AV/C devices: camcorders, set-top boxes, TV sets, various audio devices, and more +SUBSYSTEM="firewire", ATTR{specifier_id}="0x00a02d", ATTR{version}="0x010001", PROGRAM="/bin/chgrp video /dev/%P" + # libusb device nodes SUBSYSTEM="usb", ENV{DEVTYPE}="usb_device", NAME="bus/usb/$env{BUSNUM}/$env{DEVNUM}", MODE="0664"
Signature · 2 lines
Stefan Richter -===-=--= -=-= =-=-http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
Quoted 6 lines · I wrote:
This allows to set ownership, permissions, or/ and access control lists for each device file based on device type markers obtained from sysfs. For example, access to the device file of a video camera could be allowed to users who belong to a "video" group, while files of other device types (storage devices, local node...) remain accessible only to root by default. This is what this change implements.
I forgot to mention:
At least libraw1394 v2.0.1 is required to use such a device type based permissions scheme. (Current and stable is libraw1394 v2.0.2.)
IIDC cameras which are typically accessed via libdc1394 can be used via this permission scheme independently of libraw1394; a libdc1394 v2 version is required.
Signature · 2 lines
Stefan Richter -===-=--= -=-= =-=-http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 9 lines · Stefan Richter
The device capabilities are shown in child
devices of the actual devices which provide the character device files. Hence the charcter device files already exist when the "add" events happen from which we get the device type (the so-called unit architecture). Therefore the 'PROGRAM=/bin/chgrp...' directive is used instead of a simple 'GROUP=...' directive.+SUBSYSTEM="firewire", ATTR{specifier_id}="0x00a02d", \
ATTR{version}="0x00010?", PROGRAM="/bin/chgrp video /dev/%P"
Oh, this breaks all sorts of valid assumptions, like it would break rules plugging into the real device, get overwritten by a child device. And we rely on proper permissions applied when the event is send, and not an unpredictable amount of time later by a child device, and so on ...
Does the fw driver know at the point it creates the device with the node, that there are childs to create? And such properties can not be made available at the device itself? Like for USB, where we have the raw "descriptor" attribute at the device, which allows us to see all possible interfaces and apply an user access control list to the node if a matching interface type is found.
Or maybe delay all uevents until the childs are all created, and send the events only after that, so the child devices can be seen at the time the event is sent out. We do that for disks, where all partitions are fully created, before we send out the events: first for the disk, and then for all partitions.
We fixed almost all these know issues in other subsystems now and I would really like to see that solved properly, without chrgrp hacks like in the rules above. It would hit us back badly, when generic infrastructure which will expect proper event timing regarding device properties.
Thanks,
Kay
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 3 lines · Kay Sievers
Quoted 2 lines · Stefan Richter
The device capabilities are shown in child
devices of the actual devices which provide the character device files.
...
Quoted 8 lines
Quoted 2 lines
+SUBSYSTEM="firewire", ATTR{specifier_id}="0x00a02d", \
ATTR{version}="0x00010?", PROGRAM="/bin/chgrp video /dev/%P"Oh, this breaks all sorts of valid assumptions, like it would break rules plugging into the real device, get overwritten by a child device. And we rely on proper permissions applied when the event is send, and not an unpredictable amount of time later by a child device, and so on ...
:-(
[For the following discussion: fw_device is the parent which is associated with a character device file, fw_unit are the children. In principle, fw_unit's could come and go at any point during the lifetime of an fw_device, but for simplicity's sake our implementation destroys and fw_device if a unit went away or was added on the respective FireWire node, and a new fw_device is created in its stead. This simplification in internal housekeeping should also make it simpler to come up with a fix for this uevent handling issue/ attributes availability issue here.]
Quoted 6 lines
Does the fw driver know at the point it creates the device with the node, that there are childs to create? And such properties can not be made available at the device itself? Like for USB, where we have the raw "descriptor" attribute at the device, which allows us to see all possible interfaces and apply an user access control list to the node if a matching interface type is found.
With some effort, we could add some fw_device attributes which reflect fw_unit properties. We would have to encode a little bit of higher-level knowledge into the fw_device handling part; but why not if it makes life for userland considerably simpler.
Quoted 5 lines
Or maybe delay all uevents until the childs are all created, and send the events only after that, so the child devices can be seen at the time the event is sent out. We do that for disks, where all partitions are fully created, before we send out the events: first for the disk, and then for all partitions.
How would that be done? Like this?
dev_set_uevent_suppress(parent, 1); device_add(parent); for_each_unit(;;) device_add(child); dev_set_uevent_suppress(parent, 0); kobject_uevent(&parent->kobj, KOBJ_ADD);
And then, how would an udev rule look like to match attributes in the fw_device's subdirectories instead of direct attributes?
SUBSYSTEM="firewire", \
ATTR{*/specifier_id}="0x00a02d", \
ATTR{*/version}="0x00010?", \
GROUP="video"Quoted 5 lines
We fixed almost all these know issues in other subsystems now and I would really like to see that solved properly, without chrgrp hacks like in the rules above. It would hit us back badly, when generic infrastructure which will expect proper event timing regarding device properties.
Hmm, or maybe we should associate character device files with the fw_units instead? Could break Fedora's userland though. Alas I don't know in detail what Fedora does to set ACLs on /dev/fw*.
And it would remove the possibility to use the firewire-cdev ABI with nodes which do not have any unit directories, but this is a rather hypothetical case since higher-level IEEE 1394 based protocols are all coupled with unit directories.
Signature · 2 lines
Stefan Richter -===-=-== -=-= -=-http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 4 lines · Stefan Richter
With some effort, we could add some
fw_deviceattributes which reflectfw_unitproperties. We would have to encode a little bit of higher-level knowledge into thefw_devicehandling part; but why not if it makes life for userland considerably simpler.
Sounds ok to me, if we will end up with that solution.
Quoted 10 lines
Quoted 3 lines
Or maybe delay all uevents until the childs are all created, and send the events only after that, so the child devices can be seen at the time the event is sent out.
How would that be done? Like this?
dev_set_uevent_suppress(parent, 1);
device_add(parent);for_each_unit(;;)
These unit events need to be suppressed too.
Quoted 4 lines
device_add(child);
dev_set_uevent_suppress(parent, 0);
kobject_uevent(&parent->kobj,KOBJ_ADD);
And send after the device event.
Quoted 7 lines
And then, how would an udev rule look like to match attributes in the fw_device's subdirectories instead of direct attributes?
SUBSYSTEM="firewire", \
ATTR{*/specifier_id}="0x00a02d", \
ATTR{*/version}="0x00010?", \
GROUP="video"
This is always numeric, and the unit directory names are not predictable, right? Then we would need something like the "*/..." magic to udev.
Quoted 9 lines
Quoted 5 lines
We fixed almost all these know issues in other subsystems now and I would really like to see that solved properly, without chrgrp hacks like in the rules above. It would hit us back badly, when generic infrastructure which will expect proper event timing regarding device properties.
Hmm, or maybe we should associate character device files with the
fw_unitsinstead? Could break Fedora's userland though. Alas I don't know in detail what Fedora does to set ACLs on/dev/fw*.
Maybe it would be nice anyway, if some units-types can be excluded from access by normal users. Sometimes it would be nice to have something like this for USB, so we could grant access to the 3G modem of mobile phone, but not to the firmware update interface :)
Quoted 4 lines
And it would remove the possibility to use the firewire-cdev ABI with nodes which do not have any unit directories, but this is a rather hypothetical case since higher-level IEEE 1394 based protocols are all coupled with unit directories.
Maybe you can just keep current node at the device for such cases? It would just not be used in the usual setup.
Thanks,
Kay
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 12 lines · Kay Sievers
Quoted 4 lines · Stefan Richter
With some effort, we could add some
fw_deviceattributes which reflectfw_unitproperties. We would have to encode a little bit of higher-level knowledge into thefw_devicehandling part; but why not if it makes life for userland considerably simpler.Sounds ok to me, if we will end up with that solution.
Quoted 3 lines
Quoted 3 lines
Or maybe delay all uevents until the childs are all created, and send the events only after that, so the child devices can be seen at the time the event is sent out.
...
[thanks for the tips how to go about that]
...
Quoted 11 lines
Quoted 7 lines
And then, how would an udev rule look like to match attributes in the fw_device's subdirectories instead of direct attributes?
SUBSYSTEM="firewire", \ ATTR{*/specifier_id}="0x00a02d", \ ATTR{*/version}="0x00010?", \ GROUP="video"This is always numeric, and the unit directory names are not predictable, right? Then we would need something like the "*/..." magic to udev.
Yes; the "dev" (character device file) sits in devices like
/sys/devices/pci0000:00/0000:00:05.0/0000:04:00.0/fw3
while the protocol-specific attributes sit in children like
/sys/devices/pci0000:00/0000:00:05.0/0000:04:00.0/fw3/fw3.0
i.e. "%s.%d", parent_name, number; with the number monotonously increasing from 0 to n-1. There may be none to many of such children.
If there is no child at all, no problem, then the device won't be interesting to typical application software anyway.
In many cases, there will be just one child, named like the parent with .0 appended.
However, we also need to cover the case that there is more than one child. Then the attributes of all children need to be evaluated.
BTW, the possible (but luckily highly improbable) case of devices with several units of different type poses the question which policy to
follow: Paranoid = deny access unless all units are of a known good type? Or comfortable = grant access if at least one unit is of known good type? (My PROGRAM based patch actually implements the comfortable policy, in case that wasn't obvious.) The paranoid case may have the serious drawback that unknown types would prevent access to known types even if the unknown ones are actually harmless to expose.
If we keep it that way that udev has to look at children attributes (if this is feasible at all), then the policy for this mixed type case would remain entirely in userspace. But if we go the route of the 1st variant, i.e. copy children properties into parent's attributes, then we need to be either very careful to solve it without hard-wiring policy into the kernel, or end up with a bias of the kernel part towards one or the other policy.
In practice, mixed unit types is a vitually non-existing case though.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
We fixed almost all these know issues in other subsystems now and I would really like to see that solved properly, without chrgrp hacks like in the rules above. It would hit us back badly, when generic infrastructure which will expect proper event timing regarding device properties.
Quoted 8 lines
Quoted 3 lines
Hmm, or maybe we should associate character device files with the
fw_unitsinstead? Could break Fedora's userland though. Alas I don't know in detail what Fedora does to set ACLs on/dev/fw*.Maybe it would be nice anyway, if some units-types can be excluded from access by normal users. Sometimes it would be nice to have something like this for USB, so we could grant access to the 3G modem of mobile phone, but not to the firmware update interface :)
This is not possible with FireWire because the actual I/O, if not even broadcast to the entire bus, can only be addressed to the FireWire node in its entirety, not to particular units on the node. The node itself (IOW the protocols which the node implements) needs to bring the necessary separation between/ security of the units which reside together on a node.¹
However, on second thought, providing character device files per fw_unit won't work with current libraw1394 and libdc1394. They expect /dev/fw* to represent one FireWire node (i.e. fw_device) each.
------------
¹) FireWire is not just a bus after all, it's also a network. Hence device security is more like host-side security in the sense that the device is a host. So, another not too far-fetched way to deal with firewire-cdev permissions is this one:
SUBSYSTEM="firewire", GROUP="video"
Still, doing it finer grained than this would be good. For example, access to SBP-2 targets should remain a privilege, even though it would require a non-trivial effort to do anything "interesting" to an SBP-2 target from userspace.
Signature · 2 lines
Stefan Richter -===-=-== -=-= -=-http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 8 lines · Stefan Richter
If there is no child at all, no problem, then the device won't be interesting to typical application software anyway.
In many cases, there will be just one child, named like the parent with .0 appended.
However, we also need to cover the case that there is more than one child. Then the attributes of all children need to be evaluated.
We could make something like:
ATTR{$kernel.*/specifier_id}="0x00a02d"
working.
Quoted 8 lines
BTW, the possible (but luckily highly improbable) case of devices with several units of different type poses the question which policy to follow:
Paranoid = deny access unless all units are of a known good type? Or comfortable = grant access if at least one unit is of known good type? (My PROGRAM based patch actually implements the comfortable policy, in case that wasn't obvious.) The paranoid case may have the serious drawback that unknown types would prevent access to known types even if the unknown ones are actually harmless to expose.
Yeah, the "comfortable" sounds fine and most useful as a default to me.
Quoted 6 lines
If we keep it that way that udev has to look at children attributes (if this is feasible at all), then the policy for this mixed type case would remain entirely in userspace. But if we go the route of the 1st variant, i.e. copy children properties into parent's attributes, then we need to be either very careful to solve it without hard-wiring policy into the kernel, or end up with a bias of the kernel part towards one or the other policy.
For USB, we read the raw "descriptor", find all possible interfaces, get class/subclass/protocol byte values from it, remove the duplicates, and add the values to colon enclosed strings:
$ /lib/udev/usb_id -x /bus/usb/devices/2-1.1.2 | grep INTER ID_USB_INTERFACES=:030101:030000:
That way we can easily match on "*:030101:*". Maybe that's an option, if such information is available at the fw_device, at the time it is created. That would be the simplest solution for userspace.
Thanks,
Kay
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 23 lines · Kay Sievers
Quoted 8 lines · Stefan Richter
BTW, the possible (but luckily highly improbable) case of devices with several units of different type poses the question which policy to follow:
Paranoid = deny access unless all units are of a known good type? Or
comfortable = grant access if at least one unit is of known good type? (My PROGRAM based patch actually implements the comfortable policy, in case that wasn't obvious.) The paranoid case may have the serious drawback that unknown types would prevent access to known types even if the unknown ones are actually harmless to expose.Yeah, the "comfortable" sounds fine and most useful as a default to me.
Quoted 6 lines
If we keep it that way that udev has to look at children attributes (if this is feasible at all), then the policy for this mixed type case would remain entirely in userspace. But if we go the route of the 1st variant, i.e. copy children properties into parent's attributes, then we need to be either very careful to solve it without hard-wiring policy into the kernel, or end up with a bias of the kernel part towards one or the other policy.
For USB, we read the raw "descriptor", find all possible interfaces, get class/subclass/protocol byte values from it, remove the duplicates, and add the values to colon enclosed strings:
Quoted 6 lines
$ /lib/udev/usb_id -x /bus/usb/devices/2-1.1.2 | grep INTER ID_USB_INTERFACES=:030101:030000:That way we can easily match on "*:030101:*". Maybe that's an option, if such information is available at the
fw_device, at the time it is created. That would be the simplest solution for userspace.
OK, an equivalent to this could be done in two ways:
a) firewire-core is changed to defer an fw_device's add uevent until the child devices are populated. A userland helper program is introduced which walks the subdirectories and constructs information to the effect of "this device has IIDC or AV/C or whatever else known interesting units".
b) firewire-core is changed to provide a new fw_device attribute which contains a concatenation of the specifier_id/version tuples of all units of the fw_device. This attribute will be there before the "add" uevent of the fw_device. Udev rules based on this new attribute will be trivial in the "comfortable" scheme; the "paranoid" scheme can also be served by this new attribute but would apparently need a tiny helper again.
I think I'll start with b) and see how it works out.
Also, since uevent deferral looks cheap & safe, we could implement this in addition to b) to expand userland's options in dealing with firewire-core's sysfs interface.
Signature · 3 lines
Stefan Richter -===-=--= -=-= =-=- http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 6 lines · Stefan Richter
fw_deviceis the parent which is
associated with a character device file,fw_unitare the children. In principle, fw_unit's could come and go at any point during the lifetime of anfw_device, but for simplicity's sake our implementation destroys andfw_deviceif a unit went away or was added on the respective FireWire node, and a newfw_deviceis created in its stead.
Err, no, I remembered wrong, even though I wrote parts of the respective code myself... The fw_device /stays there/, while fw_units may come or go later during the fw_device's lifetime. This happens when the device signals us that its configuration has changed; we then re-read the configuration and destroy/ create fw_units.
(The only restriction of the current implementation is that we don't fully compare the old and new configuration to find out whether previous fw_units are still there after such a change; we simply destroy all previous fw_units and freshly create all current fw_units.)
However, I know of no device which actually does this, except for PC OSs like Linux when unit architecture drivers are loaded while its 1394 driver stack is already life (e.g. the Linux userspace driver Endpoint which is an SBP-2 target, ditto the eth1394 kernel driver, there have also been & are plans for AV/C target drivers --- i.e. audio or video devices for which we want group assignments and/or ACLs being updated). So, it's currently a scenario of low practical relevance, but userspace should be enabled to react on it.
From what I remember how Fedora's symlinking solution works (which
serves two purposes: create file aliases with names more meaningful than plain /dev/fw*; but more importantly, update the ACL of the device file behind the symlink), it works the same regardless whether a unit pops up 1 nanosecond or 1 day after addition of the fw_device.
I'm not sure though how to proceed for an "upstream" solution. The Fedora mechanism is surely nice, but it isn't very compact (involves an extra script and the infrastructure & policy files to set ACLs for the console owner) and does not cover the use case of Unix group ownership.
Would it work if I implement the discussed aggregate fw_device attribute (i.e. the concatenation of significant attributes of all children) and send an KOBJ_CHANGE event when units were recreated? Do change events trigger the necessary actions to update ownership and ACL?
One other thing which I forgot yesterday:
We actually already have an attribute which exists when an fw_device KOBJ_ADD event happens and includes information about all children. However, it is a binary blob (config_rom, a dump of the device description from which also ATTR{specifier_id} and ATTR{version} are derived; format is defined in IEEE 1212, contents defined in various higher level protocol specs) --- and since the firewire-core has a config ROM parser anyway, we do not really want to duplicate this parser in userspace for basic purposes like setting device file ownership/ ACLs/ symlinks and the likes.
Signature · 3 lines
Stefan Richter -===-=--= -=-= =-=- http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 4 lines · Stefan Richter
Would it work if I implement the discussed aggregate
fw_deviceattribute (i.e. the concatenation of significant attributes of all children) and send anKOBJ_CHANGEevent when units were recreated? Â Do change events trigger the necessary actions to update ownership and ACL?
Yes, that sounds fine.
Quoted 10 lines
One other thing which I forgot yesterday:
We actually already have an attribute which exists when an
fw_deviceKOBJ_ADDevent happens and includes information about all children. However, it is a binary blob (config_rom, a dump of the device description from which also ATTR{specifier_id} and ATTR{version} are derived; format is defined in IEEE 1212, contents defined in various higher level protocol specs) --- and since the firewire-core has a config ROM parser anyway, we do not really want to duplicate this parser in userspace for basic purposes like setting device file ownership/ ACLs/ symlinks and the likes.
That sounds like the USB "descriptors" file. Yes, it would be nice if we can have that in an easily readable format in syfs without the need for a parser.
Kay
PATCH 1/2 firewire: core: check for missing struct update at build time, not run time
struct fw_attribute_group.attrs.[] must have enough room for all attributes. This can and should be checked at build time.
Our previous check at run time was a little late and not reliable since most of the time less than the available attributes are populated.
Furthermore, omit an increment of an index at its last usage.
--- drivers/firewire/fw-device.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
Index: linux/drivers/firewire/fw-device.c =================================--- linux.orig/drivers/firewire/fw-device.c +++ linux/drivers/firewire/fw-device.c
@@ -289,14 +289,13 @@ static void init_fw_attribute_group(stru
attr = &config_rom_attributes[i].attr;
if (attr->show(dev, attr, NULL) < 0)
continue;
group->attrs[j++] = &attr->attr;
}
- BUG_ON(j >= ARRAY_SIZE(group->attrs));
- group->attrs[j++] = NULL;
+ group->attrs[j] = NULL;
group->groups[0] = &group->group;
group->groups[1] = NULL;
group->group.attrs = group->attrs;
dev->groups = group->groups;
}
@@ -570,9 +569,13 @@ static void create_units(struct fw_devic
unit->device.parent = &device->device; dev_set_name(&unit->device, "%s.%d", dev_name(&device->device), i++);
+ BUILD_BUG_ON(ARRAY_SIZE(unit->attribute_group.attrs) <
+ ARRAY_SIZE(fw_unit_attributes) +
+ ARRAY_SIZE(config_rom_attributes));
init_fw_attribute_group(&unit->device,
fw_unit_attributes, &unit->attribute_group);
+
if (device_register(&unit->device) < 0)
goto skip_unit;
@@ -849,9 +852,13 @@ static void fw_device_init(struct work_s
device->device.devt = MKDEV(fw_cdev_major, minor); dev_set_name(&device->device, "fw%d", minor);
+ BUILD_BUG_ON(ARRAY_SIZE(device->attribute_group.attrs) <
+ ARRAY_SIZE(fw_device_attributes) +
+ ARRAY_SIZE(config_rom_attributes));
init_fw_attribute_group(&device->device,
fw_device_attributes, &device->attribute_group);
+
if (device_add(&device->device)) {
fw_error("Failed to add device.\n"); goto error_with_cdev;
Signature · 3 lines
Stefan Richter -===-=--= -=-= =-=- http://arcgraph.de/sr/
PATCH 2/2 firewire: core: add sysfs attribute for easier udev rules
This adds the attribute /sys/bus/firewire/devices/fw[0-9]+/units. It can be used in udev rules like the following ones:
# IIDC devices: industrial cameras and some webcams SUBSYSTEM="firewire", ATTR{units}="*0x00a02d:0x00010?*", GROUP="video"
# AV/C devices: camcorders, set-top boxes, TV sets, audio devices, ... SUBSYSTEM="firewire", ATTR{units}="*0x00a02d:0x010001*", GROUP="video"
Background:
firewire-core manages two device types:
- fw_device is a FireWire node. A character device file is associated with it.
- fw_unit is a unit directory on a node. Each fw_device may have 0..n children of type fw_unit. The units tell us what kinds of protocols a node implements.
We want to set ownership or ACLs or permissions of the character device file of an fw_device, or/and create symlinks to it, based on available protocols. Until now udev rules had to look at the fw_unit devices and then modify their parent's character device file accordingly. This is problematic for two reasons: 1) It happens sometime after the creation of the fw_device, 2) an access policy may require that information from all children is evaluated before a decision about the parent is made.
Problem 1) can ultimately not be avoided since this is the nature of FireWire nodes: They may add or remove unit directories at any point in time.
However, we can still help userland a lot by providing the protocol type information of all units in a summary sysfs attribute directly at the fw_device. This way,
- the information is immediately available at the affected device when userspace goes about to handle an ADD or CHANGE event of the fw_device,
- with most policies, it won't be necessary anymore to dig through child attributes.
The new attribute is called "units". It contains space-separated tuples of specifier_id and version of each present unit. The delimiter within tuples is a colon. Specifier_id and version are printed as 0x%06x.
Here is an example of a node which implements an IPv4 unit and an IPv6
unit: $ cat /sys/bus/firewire/devices/fw2/units 0x00005e:0x000001 0x00005e:0x000002
--- drivers/firewire/fw-device.c | 49 +++++++++++++++++++++++++++++++++++++++++++ drivers/firewire/fw-device.h | 2 - 2 files changed, 50 insertions(+), 1 deletion(-)
Index: linux/drivers/firewire/fw-device.c =================================--- linux.orig/drivers/firewire/fw-device.c +++ linux/drivers/firewire/fw-device.c
@@ -355,9 +355,55 @@ static ssize_t guid_show(struct device *
return ret;
}
+static int units_sprintf(char *buf, u32 *directory) +{
+ struct fw_csr_iterator ci;
+ int key, value;
+ int specifier_id = 0;
+ int version = 0;
+
+ fw_csr_iterator_init(&ci, directory);
+ while (fw_csr_iterator_next(&ci, &key, &value)) {
+ switch (key) {
+ case CSR_SPECIFIER_ID:
+ specifier_id = value;
+ break;
+ case CSR_VERSION:
+ version = value;
+ break;
+ }
+ }
+
+ return sprintf(buf, "0x%06x:0x%06x ", specifier_id, version);
+}
+
+static ssize_t units_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fw_device *device = fw_device(dev);
+ struct fw_csr_iterator ci;
+ int key, value, i = 0;
+
+ down_read(&fw_device_rwsem);
+ fw_csr_iterator_init(&ci, &device->config_rom[5]);
+ while (fw_csr_iterator_next(&ci, &key, &value)) {
+ if (key != (CSR_UNIT | CSR_DIRECTORY))
+ continue;
+ i += units_sprintf(&buf[i], ci.p + value - 1);
+ if (i >= PAGE_SIZE - (8 + 1 + 8 + 1))
+ break;
+ }
+ up_read(&fw_device_rwsem);
+
+ sprintf(&buf[i ? i - 1 : 0], "\n");
+
+ return i;
+}
+
static struct device_attribute fw_device_attributes[] = {
__ATTR_RO(config_rom),
__ATTR_RO(guid),
+ __ATTR_RO(units),
__ATTR_NULL,
};
@@ -1000,6 +1046,9 @@ static void fw_device_refresh(struct wor
create_units(device);
+ /* Userspace may want to re-read attributes. */
+ kobject_uevent(&device->device.kobj, KOBJ_CHANGE);
+
if (atomic_cmpxchg(&device->state,
FW_DEVICE_INITIALIZING, FW_DEVICE_RUNNING) = FW_DEVICE_GONE)
Index: linux/drivers/firewire/fw-device.h =================================--- linux.orig/drivers/firewire/fw-device.h +++ linux/drivers/firewire/fw-device.h
@@ -42,7 +42,7 @@ enum fw_device_state {
struct fw_attribute_group {
struct attribute_group *groups[2];
struct attribute_group group;
- struct attribute *attrs[11];
+ struct attribute *attrs[12];
};
struct fw_node;
Signature · 3 lines
Stefan Richter -===-=--= -=-= =-=- http://arcgraph.de/sr/
Re: [PATCH 2/2] firewire: core: add sysfs attribute for easier udev rules
Review
Quoted 10 lines · Stefan Richter
(quoted diff) +10
-0+ while (fw_csr_iterator_next(&ci, &key, &value)) { + if (key != (CSR_UNIT | CSR_DIRECTORY)) + continue; + i += units_sprintf(&buf[i], ci.p + value - 1); + if (i >= PAGE_SIZE - (8 + 1 + 8 + 1)) + break; + } + up_read(&fw_device_rwsem); + + sprintf(&buf[i ? i - 1 : 0], "\n");
Rather:
if (i) buf[i - 1] = '\n';
Quoted 3 lines
(quoted diff) +3
-0+ + return i; +}
Signature · 2 lines
Stefan Richter -===-=--= -=-= =-=http://arcgraph.de/sr/
Re: [PATCH 2/2] firewire: core: add sysfs attribute for easier udev rules
In reply to Stefan RichterQuoted 6 lines · Stefan Richter
This adds the attribute /sys/bus/firewire/devices/fw[0-9]+/units. It can be used in udev rules like the following ones:
# IIDC devices: industrial cameras and some webcams SUBSYSTEM="firewire", ATTR{units}="*0x00a02d:0x00010?*", GROUP="video"
Quoted 6 lines
# AV/C devices: camcorders, set-top boxes, TV sets, audio devices, ... SUBSYSTEM="firewire", ATTR{units}="*0x00a02d:0x010001*", GROUP="video"
Here is an example of a node which implements an IPv4 unit and an IPv6
unit: $ cat/sys/bus/firewire/devices/fw2/units0x00005e:0x000001 0x00005e:0x000002
Looks great. Let me know when you are merging this, so we can put the rules in the udev tree.
Thanks,
Kay
Re: [PATCH 2/2] firewire: core: add sysfs attribute for easier udev rules
Quoted 2 lines · Kay Sievers
Looks great. Let me know when you are merging this, so we can put the rules in the udev tree.
Yes, will do. Thanks for the guidance.
Signature · 2 lines
Stefan Richter -===-=--= -=-= =-=http://arcgraph.de/sr/
PATCH 1/1 rules: set group ownership of new firewire driver device
In reply to Stefan RichterThe newer firewire-core driver exposes per-device character device files, called /dev/fw[0-9]*, in contrast to the older raw1394, video1394, dv1394 drivers which created one global file or per-controller files.
This allows to set ownership, permissions, or/ and access control lists for each device file based on device type markers obtained from sysfs. The "units" attribute which is used for this purpose has become available in Linux 2.6.31(-rc1) by commit 0210b66dd88a2a1e451901b00378a2068b6ccb35.
The added rules match identifiers of
- IIDC devices:
industrial cameras and some webcams,
- AV/C devices:
camcorders, set-top boxes, TV sets, audio devices, and similar devices.
--- rules/rules.d/50-udev-default.rules | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
rules/rules.d/50-udev-default.rules +4 -1
@@ -43,9 +43,12 @@ SUBSYSTEM="drm", GROUP="video" # DVB (video) SUBSYSTEM="dvb", ENV{DVB_ADAPTER_NUM}="?*", NAME="dvb/adapter$env{DVB_ADAPTER_NUM}/$env{DVB_DEVICE_TYPE}$env{DVB_DEVICE_NUM}", GROUP="video" -# Firewire +# FireWire (dv1394 and video1394 drivers) KERNEL="dv1394-[0-9]*", NAME="dv1394/%n", GROUP="video" KERNEL="video1394-[0-9]*", NAME="video1394/%n", GROUP="video" +# FireWire (firewire-core driver: IIDC devices, AV/C devices) +SUBSYSTEM="firewire", ATTR{units}="*0x00a02d:0x00010*", GROUP="video" +SUBSYSTEM="firewire", ATTR{units}="*0x00a02d:0x010001*", GROUP="video" # libusb device nodes SUBSYSTEM="usb", ENV{DEVTYPE}="usb_device", NAME="bus/usb/$env{BUSNUM}/$env{DEVNUM}", MODE="0664"
Signature · 6 lines
1.6.3.3 -- Stefan Richter -===-=--= -== ----http://arcgraph.de/sr/
Re: [PATCH udev] rules: set group ownership of new firewire driver
In reply to Stefan RichterQuoted 8 lines · Stefan Richter
The newer firewire-core driver exposes per-device character device files, called /dev/fw[0-9]*, in contrast to the older raw1394, video1394, dv1394 drivers which created one global file or per-controller files.
This allows to set ownership, permissions, or/ and access control lists for each device file based on device type markers obtained from sysfs. The "units" attribute which is used for this purpose has become available in Linux 2.6.31(-rc1) by commit
0210b66dd88a2a1e451901b00378a2068b6ccb35.
Looks nice. Applied.
Thanks,
Kay