1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-27 14:03:43 +03:00

Merge pull request #20042 from yuwata/udev-rule-fix-cid-1457766

udev: fix an issue and add a testcase
This commit is contained in:
Luca Boccassi 2021-06-26 23:03:36 +01:00 committed by GitHub
commit 56175bc45d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 21 deletions

View File

@ -1539,10 +1539,6 @@ static int udev_rule_apply_token_to_event(
Hashmap *properties_list) {
UdevRuleToken *token;
char buf[UDEV_PATH_SIZE];
const char *val;
size_t count;
bool match;
int r;
assert(rules);
@ -1566,33 +1562,45 @@ static int udev_rule_apply_token_to_event(
return token_match_string(token, device_action_to_string(a));
}
case TK_M_DEVPATH:
case TK_M_DEVPATH: {
const char *val;
r = sd_device_get_devpath(dev, &val);
if (r < 0)
return log_rule_error_errno(dev, rules, r, "Failed to get devpath: %m");
return token_match_string(token, val);
}
case TK_M_KERNEL:
case TK_M_PARENTS_KERNEL:
case TK_M_PARENTS_KERNEL: {
const char *val;
r = sd_device_get_sysname(dev, &val);
if (r < 0)
return log_rule_error_errno(dev, rules, r, "Failed to get sysname: %m");
return token_match_string(token, val);
case TK_M_DEVLINK:
}
case TK_M_DEVLINK: {
const char *val;
FOREACH_DEVICE_DEVLINK(dev, val)
if (token_match_string(token, strempty(startswith(val, "/dev/"))))
return token->op == OP_MATCH;
return token->op == OP_NOMATCH;
}
case TK_M_NAME:
return token_match_string(token, event->name);
case TK_M_ENV:
case TK_M_ENV: {
const char *val;
if (sd_device_get_property_value(dev, token->data, &val) < 0)
val = hashmap_get(properties_list, token->data);
return token_match_string(token, val);
}
case TK_M_CONST: {
const char *k = token->data;
const char *val, *k = token->data;
if (streq(k, "arch"))
val = architecture_to_string(uname_architecture());
@ -1603,13 +1611,18 @@ static int udev_rule_apply_token_to_event(
return token_match_string(token, val);
}
case TK_M_TAG:
case TK_M_PARENTS_TAG:
case TK_M_PARENTS_TAG: {
const char *val;
FOREACH_DEVICE_TAG(dev, val)
if (token_match_string(token, val))
return token->op == OP_MATCH;
return token->op == OP_NOMATCH;
}
case TK_M_SUBSYSTEM:
case TK_M_PARENTS_SUBSYSTEM:
case TK_M_PARENTS_SUBSYSTEM: {
const char *val;
r = sd_device_get_subsystem(dev, &val);
if (r == -ENOENT)
val = NULL;
@ -1617,8 +1630,11 @@ static int udev_rule_apply_token_to_event(
return log_rule_error_errno(dev, rules, r, "Failed to get subsystem: %m");
return token_match_string(token, val);
}
case TK_M_DRIVER:
case TK_M_PARENTS_DRIVER:
case TK_M_PARENTS_DRIVER: {
const char *val;
r = sd_device_get_driver(dev, &val);
if (r == -ENOENT)
val = NULL;
@ -1626,11 +1642,13 @@ static int udev_rule_apply_token_to_event(
return log_rule_error_errno(dev, rules, r, "Failed to get driver: %m");
return token_match_string(token, val);
}
case TK_M_ATTR:
case TK_M_PARENTS_ATTR:
return token_match_attr(token, dev, event);
case TK_M_SYSCTL: {
_cleanup_free_ char *value = NULL;
char buf[UDEV_PATH_SIZE];
(void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false);
r = sysctl_read(sysctl_normalize(buf), &value);
@ -1641,12 +1659,15 @@ static int udev_rule_apply_token_to_event(
}
case TK_M_TEST: {
mode_t mode = PTR_TO_MODE(token->data);
char buf[UDEV_PATH_SIZE];
struct stat statbuf;
bool match;
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
if (!path_is_absolute(buf) &&
udev_resolve_subsys_kernel(buf, buf, sizeof(buf), false) < 0) {
char tmp[UDEV_PATH_SIZE];
const char *val;
r = sd_device_get_syspath(dev, &val);
if (r < 0)
@ -1672,7 +1693,8 @@ static int udev_rule_apply_token_to_event(
return token->op == (match ? OP_MATCH : OP_NOMATCH);
}
case TK_M_PROGRAM: {
char result[UDEV_LINE_SIZE];
char buf[UDEV_PATH_SIZE], result[UDEV_LINE_SIZE];
size_t count;
event->program_result = mfree(event->program_result);
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
@ -1698,6 +1720,7 @@ static int udev_rule_apply_token_to_event(
}
case TK_M_IMPORT_FILE: {
_cleanup_fclose_ FILE *f = NULL;
char buf[UDEV_PATH_SIZE];
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
log_rule_debug(dev, rules, "Importing properties from '%s'", buf);
@ -1744,7 +1767,7 @@ static int udev_rule_apply_token_to_event(
}
case TK_M_IMPORT_PROGRAM: {
_cleanup_strv_free_ char **lines = NULL;
char result[UDEV_LINE_SIZE], **line;
char buf[UDEV_PATH_SIZE], result[UDEV_LINE_SIZE], **line;
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
log_rule_debug(dev, rules, "Importing properties from results of '%s'", buf);
@ -1789,6 +1812,7 @@ static int udev_rule_apply_token_to_event(
UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data);
assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX);
unsigned mask = 1U << (int) cmd;
char buf[UDEV_PATH_SIZE];
if (udev_builtin_run_once(cmd)) {
/* check if we ran already */
@ -1814,6 +1838,8 @@ static int udev_rule_apply_token_to_event(
return token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH);
}
case TK_M_IMPORT_DB: {
const char *val;
if (!event->dev_db_clone)
return token->op == OP_NOMATCH;
r = sd_device_get_property_value(event->dev_db_clone, token->value, &val);
@ -1848,6 +1874,8 @@ static int udev_rule_apply_token_to_event(
return token->op == OP_MATCH;
}
case TK_M_IMPORT_PARENT: {
char buf[UDEV_PATH_SIZE];
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
r = import_parent_into_properties(dev, buf);
if (r < 0)
@ -2006,9 +2034,9 @@ static int udev_rule_apply_token_to_event(
break;
}
case TK_A_ENV: {
const char *name = token->data;
const char *val, *name = token->data;
char value_new[UDEV_NAME_SIZE], *p = value_new;
size_t l = sizeof(value_new);
size_t count, l = sizeof(value_new);
if (isempty(token->value)) {
if (token->op == OP_ADD)
@ -2025,7 +2053,7 @@ static int udev_rule_apply_token_to_event(
(void) udev_event_apply_format(event, token->value, p, l, false);
if (event->esc == ESCAPE_REPLACE) {
count = udev_replace_chars(buf, NULL);
count = udev_replace_chars(p, NULL);
if (count > 0)
log_rule_debug(dev, rules, "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"",
count, name, token->op == OP_ADD ? "+" : "", token->value);
@ -2037,6 +2065,8 @@ static int udev_rule_apply_token_to_event(
break;
}
case TK_A_TAG: {
char buf[UDEV_PATH_SIZE];
(void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false);
if (token->op == OP_ASSIGN)
device_cleanup_tags(dev);
@ -2055,6 +2085,9 @@ static int udev_rule_apply_token_to_event(
break;
}
case TK_A_NAME: {
char buf[UDEV_PATH_SIZE];
size_t count;
if (event->name_final)
break;
if (token->op == OP_ASSIGN_FINAL)
@ -2085,7 +2118,8 @@ static int udev_rule_apply_token_to_event(
break;
}
case TK_A_DEVLINK: {
char *p;
char buf[UDEV_PATH_SIZE], *p;
size_t count;
if (event->devlink_final)
break;
@ -2129,8 +2163,8 @@ static int udev_rule_apply_token_to_event(
break;
}
case TK_A_ATTR: {
const char *key_name = token->data;
char value[UDEV_NAME_SIZE];
char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
const char *val, *key_name = token->data;
if (udev_resolve_subsys_kernel(key_name, buf, sizeof(buf), false) < 0 &&
sd_device_get_syspath(dev, &val) >= 0)
@ -2154,7 +2188,7 @@ static int udev_rule_apply_token_to_event(
break;
}
case TK_A_SYSCTL: {
char value[UDEV_NAME_SIZE];
char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE];
(void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false);
(void) udev_event_apply_format(event, token->value, value, sizeof(value), false);
@ -2168,6 +2202,7 @@ static int udev_rule_apply_token_to_event(
case TK_A_RUN_BUILTIN:
case TK_A_RUN_PROGRAM: {
_cleanup_free_ char *cmd = NULL;
char buf[UDEV_PATH_SIZE];
if (event->run_final)
break;

View File

@ -1162,6 +1162,21 @@ EOF
rules => <<EOF
ENV{WITH_WS}=" one two three "
SYMLINK=" first name-\$env{WITH_WS}-end another_symlink a b c "
EOF
},
{
desc => "symlink with env which contain slash (see #19309)",
devices => [
{
devpath => "/devices/pci0000:00/0000:00:1d.7/usb5/5-2/5-2:1.0/tty/ttyACM0",
exp_links => ["first", "name-aaa_bbb_ccc-end",
"another_symlink", "a", "b", "c"],
not_exp_links => ["ame-aaa/bbb/ccc-end"],
}],
rules => <<EOF
ENV{WITH_SLASH}="aaa/bbb/ccc"
OPTIONS="string_escape=replace", ENV{REPLACED}="\$env{WITH_SLASH}"
SYMLINK=" first name-\$env{REPLACED}-end another_symlink a b c "
EOF
},
{