udev: allow/denylist for reading sysfs attributes when composing a NIC name

Users can currently pick specific versions of NIC naming, but that
does not guarantee that NIC names won't change after the kernel adds
a new sysfs attribute.

This patch allows for an allow/deny list of sysfs attributes
that could be used when composing the name.

These lists can be supplied as an hwdb entry in the form of
/etc/udev/hwdb.d/50-net-naming-allowlist.hwdb
net:naming:drvirtio_net
  ID_NET_NAME_ALLOW=0
  ID_NET_NAME_ALLOW_ACPI_INDEX=1
  ID_NET_NAME_ALLOW_ADDR_ASSIGN_TYPE=1
  ID_NET_NAME_ALLOW_ADDRESS=1
  ID_NET_NAME_ALLOW_ARI_ENABLED=1
  ID_NET_NAME_ALLOW_DEV_PORT=1
  ID_NET_NAME_ALLOW_FUNCTION_ID=1
  ID_NET_NAME_ALLOW_IFLINK=1
  ID_NET_NAME_ALLOW_INDEX=1
  ID_NET_NAME_ALLOW_LABEL=1
  ID_NET_NAME_ALLOW_PHYS_PORT_NAME=1
  ID_NET_NAME_ALLOW_TYPE=1
This commit is contained in:
Lukas Nykryn
2023-12-19 19:15:52 +09:00
committed by Yu Watanabe
parent 91436851d5
commit 3b2e7dc5a2
5 changed files with 180 additions and 19 deletions
+81
View File
@@ -1,6 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "sd-device.h"
#include "alloc-util.h"
#include "device-private.h"
#include "netif-naming-scheme.h"
#include "proc-cmdline.h"
#include "string-util.h"
@@ -101,3 +104,81 @@ static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
};
DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
static int naming_sysattr_allowed_by_default(sd_device *dev) {
int r;
assert(dev);
r = device_get_property_bool(dev, "ID_NET_NAME_ALLOW");
if (r == -ENOENT)
return true;
return r;
}
static int naming_sysattr_allowed(sd_device *dev, const char *sysattr) {
char *sysattr_property;
int r;
assert(dev);
assert(sysattr);
sysattr_property = strjoina("ID_NET_NAME_ALLOW_", sysattr);
ascii_strupper(sysattr_property);
r = device_get_property_bool(dev, sysattr_property);
if (r == -ENOENT)
/* If ID_NET_NAME_ALLOW is not set or set to 1 default is to allow */
return naming_sysattr_allowed_by_default(dev);
return r;
}
int device_get_sysattr_int_filtered(sd_device *device, const char *sysattr, int *ret_value) {
int r;
r = naming_sysattr_allowed(device, sysattr);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
return device_get_sysattr_int(device, sysattr, ret_value);
}
int device_get_sysattr_unsigned_filtered(sd_device *device, const char *sysattr, unsigned *ret_value) {
int r;
r = naming_sysattr_allowed(device, sysattr);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
return device_get_sysattr_unsigned(device, sysattr, ret_value);
}
int device_get_sysattr_bool_filtered(sd_device *device, const char *sysattr) {
int r;
r = naming_sysattr_allowed(device, sysattr);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
return device_get_sysattr_bool(device, sysattr);
}
int device_get_sysattr_value_filtered(sd_device *device, const char *sysattr, const char **ret_value) {
int r;
r = naming_sysattr_allowed(device, sysattr);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
return sd_device_get_sysattr_value(device, sysattr, ret_value);
}
+7
View File
@@ -3,6 +3,8 @@
#include <stdbool.h>
#include "sd-device.h"
#include "macro.h"
/* So here's the deal: net_id is supposed to be an exercise in providing stable names for network devices. However, we
@@ -95,3 +97,8 @@ NamePolicy name_policy_from_string(const char *p) _pure_;
const char *alternative_names_policy_to_string(NamePolicy p) _const_;
NamePolicy alternative_names_policy_from_string(const char *p) _pure_;
int device_get_sysattr_int_filtered(sd_device *device, const char *sysattr, int *ret_value);
int device_get_sysattr_unsigned_filtered(sd_device *device, const char *sysattr, unsigned *ret_value);
int device_get_sysattr_bool_filtered(sd_device *device, const char *sysattr);
int device_get_sysattr_value_filtered(sd_device *device, const char *sysattr, const char **ret_value);