Skip to contentSkip to conversation

thunderbolt: For dev authorization changes, include the actual event in udev change notification

PATCH Superseded Rajat Jain 3 participants 5 messages Jul 24, 2021 – Jul 30, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Rajat Jain

author
#0 lore raw

PATCH v2 1/1 thunderbolt: For dev authorization changes, include the actual event in udev change notification

For security, we would like to monitor and track when the thunderbolt devices are authorized and deauthorized (i.e. when the thunderbolt sysfs "authorized" attribute changes). Currently the userspace gets a udev change notification when there is a change, but the state may have changed (again) by the time we look at the authorized attribute in sysfs. So an authorization event may go unnoticed. Thus make it easier by informing the actual change (new value of authorized attribute) in the udev change notification.

The change is included as a key value "authorized=<val>" where <val> is the new value of sysfs attribute "authorized", and is described at Documentation/ABI/testing/sysfs-bus-thunderbolt under /sys/bus/thunderbolt/devices/.../authorized

---
 drivers/thunderbolt/switch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/thunderbolt/switch.c +6 -2
index 83b1ef3d5d03..382128dfbdee 100644
@@ -1499,6 +1499,7 @@ static ssize_t authorized_show(struct device *dev,
 static int disapprove_switch(struct device *dev, void *not_used)
 {
 	struct tb_switch *sw;
+	char *envp[] = { "AUTHORIZED=0", NULL };
 
 	sw = tb_to_switch(dev);
 	if (sw && sw->authorized) {
@@ -1514,7 +1515,7 @@ static int disapprove_switch(struct device *dev, void *not_used)
 			return ret;
 
 		sw->authorized = 0;
-		kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+		kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
 	}
 
 	return 0;
@@ -1523,6 +1524,8 @@ static int disapprove_switch(struct device *dev, void *not_used)
 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
 {
 	int ret = -EINVAL;
+	char envp_string[13];
+	char *envp[] = { envp_string, NULL };
 
 	if (!mutex_trylock(&sw->tb->lock))
 		return restart_syscall();
@@ -1560,7 +1563,8 @@ static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
 	if (!ret) {
 		sw->authorized = val;
 		/* Notify status change to the userspace */
-		kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+		sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
+		kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
 	}
 
 unlock:
Signature · 1 line
2.32.0.432.gabb21c7263-goog
author
#1 lore raw

(fixing the typo in the email ID for Greg).

Quoted 63 lines · Rajat Jain

For security, we would like to monitor and track when the thunderbolt devices are authorized and deauthorized (i.e. when the thunderbolt sysfs "authorized" attribute changes). Currently the userspace gets a udev change notification when there is a change, but the state may have changed (again) by the time we look at the authorized attribute in sysfs. So an authorization event may go unnoticed. Thus make it easier by informing the actual change (new value of authorized attribute) in the udev change notification.

The change is included as a key value "authorized=<val>" where <val> is the new value of sysfs attribute "authorized", and is described at Documentation/ABI/testing/sysfs-bus-thunderbolt under /sys/bus/thunderbolt/devices/.../authorized

---
 drivers/thunderbolt/switch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/thunderbolt/switch.c +6 -3
index 83b1ef3d5d03..382128dfbdee 100644
@@ -1499,6 +1499,7 @@ static ssize_t authorized_show(struct device *dev,
 static int disapprove_switch(struct device *dev, void *not_used)
 {
        struct tb_switch *sw;
+       char *envp[] = { "AUTHORIZED=0", NULL };
 
        sw = tb_to_switch(dev);
        if (sw && sw->authorized) {
@@ -1514,7 +1515,7 @@ static int disapprove_switch(struct device *dev, void *not_used)
                        return ret;
 
                sw->authorized = 0;
-               kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+               kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
        }
 
        return 0;
@@ -1523,6 +1524,8 @@ static int disapprove_switch(struct device *dev, void *not_used)
 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
 {
        int ret = -EINVAL;
+       char envp_string[13];
+       char *envp[] = { envp_string, NULL };
 
        if (!mutex_trylock(&sw->tb->lock))
                return restart_syscall();
@@ -1560,7 +1563,8 @@ static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
        if (!ret) {
                sw->authorized = val;
                /* Notify status change to the userspace */
-               kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+               sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
+               kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
        }
 
 unlock:
--

2.32.0.432.gabb21c7263-goog

#2 lore raw

Hi Rajat,

Quoted 17 lines · Rajat Jain

(fixing the typo in the email ID for Greg).

Quoted 14 lines · Rajat Jain

For security, we would like to monitor and track when the thunderbolt devices are authorized and deauthorized (i.e. when the thunderbolt sysfs "authorized" attribute changes). Currently the userspace gets a udev change notification when there is a change, but the state may have changed (again) by the time we look at the authorized attribute in sysfs. So an authorization event may go unnoticed. Thus make it easier by informing the actual change (new value of authorized attribute) in the udev change notification.

The change is included as a key value "authorized=<val>" where <val> is the new value of sysfs attribute "authorized", and is described at Documentation/ABI/testing/sysfs-bus-thunderbolt under /sys/bus/thunderbolt/devices/.../authorized

Looking good, a couple of minor nits below.

Quoted 6 lines
Quoted 6 lines
---
 drivers/thunderbolt/switch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Quoted 9 lines

drivers/thunderbolt/switch.c static ssize_t authorized_show(struct device *dev, · line 1502

Quoted 9 lines
drivers/thunderbolt/switch.c +1 -0
index 83b1ef3d5d03..382128dfbdee 100644
@@ -1499,6 +1499,7 @@ static ssize_t authorized_show(struct device *dev,
 static int disapprove_switch(struct device *dev, void *not_used)
 {
        struct tb_switch *sw;
+       char *envp[] = { "AUTHORIZED=0", NULL };

Can you move arrange this to be before sw, like:

	char *envp[] = { "AUTHORIZED=0", NULL };
	struct tb_switch *sw;
Quoted 11 lines
Quoted 11 lines
drivers/thunderbolt/switch.c +1 -1
        sw = tb_to_switch(dev);
        if (sw && sw->authorized) {
@@ -1514,7 +1515,7 @@ static int disapprove_switch(struct device *dev, void *not_used)
                        return ret;
 
                sw->authorized = 0;
-               kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+               kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
        }
 
Quoted 7 lines

drivers/thunderbolt/switch.c static int disapprove_switch(struct device *dev, void *not_used) · line 1528

Quoted 7 lines
drivers/thunderbolt/switch.c +2 -0
        return 0;
@@ -1523,6 +1524,8 @@ static int disapprove_switch(struct device *dev, void *not_used)
 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
 {
        int ret = -EINVAL;
+       char envp_string[13];
+       char *envp[] = { envp_string, NULL };

Ditto.

Quoted 16 lines
Quoted 16 lines
drivers/thunderbolt/switch.c +2 -2
        if (!mutex_trylock(&sw->tb->lock))
                return restart_syscall();
@@ -1560,7 +1563,8 @@ static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
        if (!ret) {
                sw->authorized = val;
                /* Notify status change to the userspace */
-               kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+               sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
+               kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
        }
 
 unlock:
--

2.32.0.432.gabb21c7263-goog

author
#3 lore raw
Quoted 30 lines · Mika Westerberg

Hi Rajat,

Quoted 17 lines · Rajat Jain

(fixing the typo in the email ID for Greg).

Quoted 14 lines · Rajat Jain

For security, we would like to monitor and track when the thunderbolt devices are authorized and deauthorized (i.e. when the thunderbolt sysfs "authorized" attribute changes). Currently the userspace gets a udev change notification when there is a change, but the state may have changed (again) by the time we look at the authorized attribute in sysfs. So an authorization event may go unnoticed. Thus make it easier by informing the actual change (new value of authorized attribute) in the udev change notification.

The change is included as a key value "authorized=<val>" where <val> is the new value of sysfs attribute "authorized", and is described at Documentation/ABI/testing/sysfs-bus-thunderbolt under /sys/bus/thunderbolt/devices/.../authorized

Looking good, a couple of minor nits below.

Quoted 6 lines
Quoted 6 lines
---
 drivers/thunderbolt/switch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Quoted 11 lines
Quoted 9 lines

drivers/thunderbolt/switch.c static ssize_t authorized_show(struct device *dev, · line 1502

Quoted 9 lines
drivers/thunderbolt/switch.c +1 -0
index 83b1ef3d5d03..382128dfbdee 100644
@@ -1499,6 +1499,7 @@ static ssize_t authorized_show(struct device *dev,
 static int disapprove_switch(struct device *dev, void *not_used)
 {
        struct tb_switch *sw;
+       char *envp[] = { "AUTHORIZED=0", NULL };

Can you move arrange this to be before sw, like:

Done.

Quoted 15 lines
        char *envp[] = { "AUTHORIZED=0", NULL };
        struct tb_switch *sw;
Quoted 11 lines
Quoted 11 lines
drivers/thunderbolt/switch.c +1 -1
        sw = tb_to_switch(dev);
        if (sw && sw->authorized) {
@@ -1514,7 +1515,7 @@ static int disapprove_switch(struct device *dev, void *not_used)
                        return ret;
 
                sw->authorized = 0;
-               kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+               kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
        }
 
Quoted 9 lines
Quoted 7 lines

drivers/thunderbolt/switch.c static int disapprove_switch(struct device *dev, void *not_used) · line 1528

Quoted 7 lines
drivers/thunderbolt/switch.c +2 -0
        return 0;
@@ -1523,6 +1524,8 @@ static int disapprove_switch(struct device *dev, void *not_used)
 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
 {
        int ret = -EINVAL;
+       char envp_string[13];
+       char *envp[] = { envp_string, NULL };

Ditto.

Done. I still needed to define envp_string before envp (because it is used in initialization).

I sent out a v3 with these changes.

Thanks,

Rajat

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
drivers/thunderbolt/switch.c +2 -2
        if (!mutex_trylock(&sw->tb->lock))
                return restart_syscall();
@@ -1560,7 +1563,8 @@ static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
        if (!ret) {
                sw->authorized = val;
                /* Notify status change to the userspace */
-               kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
+               sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
+               kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
        }
 
 unlock:
--

2.32.0.432.gabb21c7263-goog

#4 lore raw
In reply to Rajat Jain
Quoted 14 lines · Rajat Jain

For security, we would like to monitor and track when the thunderbolt devices are authorized and deauthorized (i.e. when the thunderbolt sysfs "authorized" attribute changes). Currently the userspace gets a udev change notification when there is a change, but the state may have changed (again) by the time we look at the authorized attribute in sysfs. So an authorization event may go unnoticed. Thus make it easier by informing the actual change (new value of authorized attribute) in the udev change notification.

The change is included as a key value "authorized=<val>" where <val> is the new value of sysfs attribute "authorized", and is described at Documentation/ABI/testing/sysfs-bus-thunderbolt under /sys/bus/thunderbolt/devices/.../authorized

Quoted 4 lines
---
 drivers/thunderbolt/switch.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/SubmittingPatches for what needs to be done here to properly describe this.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot