| 1 | From 9510aa4aebf32678884f55ae251e54012a354ed1 Mon Sep 17 00:00:00 2001
|
| 2 | From: =?UTF-8?q?Alice=20=E2=9C=A8=F0=9F=8C=99=20Luna?= <[email protected]>
|
| 3 | Date: Sat, 22 Aug 2026 16:26:42 +0200
|
| 4 | Subject: [PATCH] Use sysfs_emit and field width specifier '*'
|
| 5 |
|
| 6 | This fixes the build due to strncpy being removed from the kernel 7.2
|
| 7 | and also makes the code more readable,
|
| 8 | since the handling of buffer overflows id done by sysfs_emit.
|
| 9 |
|
| 10 | Note that the documentation about reading attributes says:
|
| 11 |
|
| 12 | > New implementations of show() methods should only use sysfs_emit()
|
| 13 | > or sysfs_emit_at() when formatting the value to be returned to user space.
|
| 14 |
|
| 15 | See: https://docs.kernel.org/filesystems/sysfs.html#reading-writing-attribute-data
|
| 16 | from kernel 7.2.0, last revised 16 August 2011.
|
| 17 |
|
| 18 | This change also uses the field width specifier '*' for add_uevent_var calls.
|
| 19 |
|
| 20 | Source: https://gitlab.com/ddcci-driver-linux/ddcci-driver-linux/-/merge_requests/21.patch
|
| 21 | Upstream-Status: https://gitlab.com/ddcci-driver-linux/ddcci-driver-linux/-/merge_requests/21
|
| 22 |
|
| 23 | ---
|
| 24 | ddcci/ddcci.c | 153 +++++++++++++++++---------------------------------
|
| 25 | 1 file changed, 50 insertions(+), 103 deletions(-)
|
| 26 |
|
| 27 | diff --git a/ddcci/ddcci.c b/ddcci/ddcci.c
|
| 28 | index 6b41eab..76a1cd3 100644
|
| 29 | |
| 30 | |
| 31 | @@ -735,154 +735,101 @@ static ssize_t ddcci_attr_capabilities_show(struct device *dev,
|
| 32 | char *buf)
|
| 33 | {
|
| 34 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 35 | - ssize_t ret = -ENOENT;
|
| 36 | - size_t len;
|
| 37 |
|
| 38 | if (likely(device != NULL)) {
|
| 39 | - len = device->capabilities_len;
|
| 40 | - if (unlikely(len > PAGE_SIZE))
|
| 41 | - len = PAGE_SIZE;
|
| 42 | - if (len == 0) {
|
| 43 | - ret = len;
|
| 44 | - } else {
|
| 45 | - memcpy(buf, device->capabilities, len);
|
| 46 | - if (likely(len < PAGE_SIZE)) {
|
| 47 | - buf[len] = '\n';
|
| 48 | - ret = len+1;
|
| 49 | - }
|
| 50 | - }
|
| 51 | - }
|
| 52 | + if (device->capabilities_len == 0)
|
| 53 | + return 0;
|
| 54 |
|
| 55 | - return ret;
|
| 56 | + return sysfs_emit(buf, "%.*s\n", (int)device->capabilities_len, device->capabilities);
|
| 57 | + }
|
| 58 | + return -ENOENT;
|
| 59 | }
|
| 60 |
|
| 61 | static ssize_t ddcci_attr_prot_show(struct device *dev,
|
| 62 | struct device_attribute *attr, char *buf)
|
| 63 | {
|
| 64 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 65 | - ssize_t ret = -ENOENT;
|
| 66 | - size_t len;
|
| 67 |
|
| 68 | if (likely(device != NULL)) {
|
| 69 | - len = strnlen(device->prot, sizeof(device->prot));
|
| 70 | - strncpy(buf, device->prot, PAGE_SIZE);
|
| 71 | - if (len == 0) {
|
| 72 | - ret = len;
|
| 73 | - } else if (likely(len < PAGE_SIZE)) {
|
| 74 | - buf[len] = '\n';
|
| 75 | - ret = len+1;
|
| 76 | - } else {
|
| 77 | - ret = PAGE_SIZE;
|
| 78 | - }
|
| 79 | + if (device->prot[0] == '\0')
|
| 80 | + return 0;
|
| 81 | +
|
| 82 | + return sysfs_emit(buf, "%.*s\n", (int)sizeof(device->prot), device->prot);
|
| 83 | }
|
| 84 | - return ret;
|
| 85 | + return -ENOENT;
|
| 86 | }
|
| 87 |
|
| 88 | static ssize_t ddcci_attr_type_show(struct device *dev,
|
| 89 | struct device_attribute *attr, char *buf)
|
| 90 | {
|
| 91 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 92 | - ssize_t ret = -ENOENT;
|
| 93 | - size_t len;
|
| 94 |
|
| 95 | if (likely(device != NULL)) {
|
| 96 | - len = strnlen(device->type, sizeof(device->type));
|
| 97 | - strncpy(buf, device->type, PAGE_SIZE);
|
| 98 | - if (len == 0) {
|
| 99 | - ret = len;
|
| 100 | - } else if (likely(len < PAGE_SIZE)) {
|
| 101 | - buf[len] = '\n';
|
| 102 | - ret = len+1;
|
| 103 | - } else {
|
| 104 | - ret = PAGE_SIZE;
|
| 105 | - }
|
| 106 | + if (device->type[0] == '\0')
|
| 107 | + return 0;
|
| 108 | +
|
| 109 | + return sysfs_emit(buf, "%.*s\n", (int)sizeof(device->type), device->type);
|
| 110 | }
|
| 111 | - return ret;
|
| 112 | + return -ENOENT;
|
| 113 | }
|
| 114 |
|
| 115 | static ssize_t ddcci_attr_model_show(struct device *dev,
|
| 116 | struct device_attribute *attr, char *buf)
|
| 117 | {
|
| 118 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 119 | - ssize_t ret = -ENOENT;
|
| 120 | - size_t len;
|
| 121 |
|
| 122 | if (likely(device != NULL)) {
|
| 123 | - len = strnlen(device->model, sizeof(device->model));
|
| 124 | - strncpy(buf, device->model, PAGE_SIZE);
|
| 125 | - if (len == 0) {
|
| 126 | - ret = len;
|
| 127 | - } else if (likely(len < PAGE_SIZE)) {
|
| 128 | - buf[len] = '\n';
|
| 129 | - ret = len+1;
|
| 130 | - } else {
|
| 131 | - ret = PAGE_SIZE;
|
| 132 | - }
|
| 133 | + if (device->model[0] == '\0')
|
| 134 | + return 0;
|
| 135 | +
|
| 136 | + return sysfs_emit(buf, "%.*s\n", (int)sizeof(device->model), device->model);
|
| 137 | }
|
| 138 | - return ret;
|
| 139 | + return -ENOENT;
|
| 140 | }
|
| 141 |
|
| 142 | static ssize_t ddcci_attr_vendor_show(struct device *dev,
|
| 143 | struct device_attribute *attr, char *buf)
|
| 144 | {
|
| 145 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 146 | - ssize_t ret = -ENOENT;
|
| 147 | - size_t len;
|
| 148 |
|
| 149 | if (likely(device != NULL)) {
|
| 150 | - len = strnlen(device->vendor, sizeof(device->vendor));
|
| 151 | - strncpy(buf, device->vendor, PAGE_SIZE);
|
| 152 | - if (len == 0) {
|
| 153 | - ret = len;
|
| 154 | - } else if (likely(len < PAGE_SIZE)) {
|
| 155 | - buf[len] = '\n';
|
| 156 | - ret = len+1;
|
| 157 | - } else {
|
| 158 | - ret = PAGE_SIZE;
|
| 159 | - }
|
| 160 | + if (device->vendor[0] == '\0')
|
| 161 | + return 0;
|
| 162 | +
|
| 163 | + return sysfs_emit(buf, "%.*s\n", (int)sizeof(device->vendor), device->vendor);
|
| 164 | }
|
| 165 | - return ret;
|
| 166 | + return -ENOENT;
|
| 167 | }
|
| 168 |
|
| 169 | static ssize_t ddcci_attr_module_show(struct device *dev,
|
| 170 | struct device_attribute *attr, char *buf)
|
| 171 | {
|
| 172 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 173 | - ssize_t ret = -ENOENT;
|
| 174 | - size_t len;
|
| 175 |
|
| 176 | if (likely(device != NULL)) {
|
| 177 | - len = strnlen(device->module, sizeof(device->module));
|
| 178 | - strncpy(buf, device->module, PAGE_SIZE);
|
| 179 | - if (len == 0) {
|
| 180 | - ret = len;
|
| 181 | - } else if (likely(len < PAGE_SIZE)) {
|
| 182 | - buf[len] = '\n';
|
| 183 | - ret = len+1;
|
| 184 | - } else {
|
| 185 | - ret = PAGE_SIZE;
|
| 186 | - }
|
| 187 | + if (device->module[0] == '\0')
|
| 188 | + return 0;
|
| 189 | +
|
| 190 | + return sysfs_emit(buf, "%.*s\n", (int)sizeof(device->module), device->module);
|
| 191 | }
|
| 192 | - return ret;
|
| 193 | + return -ENOENT;
|
| 194 | }
|
| 195 |
|
| 196 | static ssize_t ddcci_attr_serial_show(struct device *dev,
|
| 197 | struct device_attribute *attr, char *buf)
|
| 198 | {
|
| 199 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 200 | - ssize_t ret = -ENOENT;
|
| 201 |
|
| 202 | if (likely(device != NULL))
|
| 203 | - ret = scnprintf(buf, PAGE_SIZE, "%d\n", device->device_number);
|
| 204 | + return sysfs_emit(buf, "%d\n", device->device_number);
|
| 205 |
|
| 206 | - return ret;
|
| 207 | + return -ENOENT;
|
| 208 | }
|
| 209 |
|
| 210 | static ssize_t ddcci_attr_modalias_show(struct device *dev,
|
| 211 | struct device_attribute *attr, char *buf)
|
| 212 | {
|
| 213 | struct ddcci_device *device = ddcci_verify_device(dev);
|
| 214 | - ssize_t ret = -ENOENT;
|
| 215 | char model[ARRAY_SIZE(device->model)];
|
| 216 | char vendor[ARRAY_SIZE(device->model)];
|
| 217 | char module[ARRAY_SIZE(device->model)];
|
| 218 | @@ -895,16 +842,16 @@ static ssize_t ddcci_attr_modalias_show(struct device *dev,
|
| 219 | ddcci_modalias_clean(vendor, sizeof(vendor), '_');
|
| 220 | ddcci_modalias_clean(module, sizeof(module), '_');
|
| 221 |
|
| 222 | - ret = scnprintf(buf, PAGE_SIZE, "%s%s-%s-%s-%s-%s\n",
|
| 223 | + return sysfs_emit(buf, "%s%.*s-%.*s-%.*s-%.*s-%.*s\n",
|
| 224 | DDCCI_MODULE_PREFIX,
|
| 225 | - device->prot,
|
| 226 | - device->type,
|
| 227 | - model,
|
| 228 | - vendor,
|
| 229 | - module
|
| 230 | + (int)sizeof(device->prot), device->prot,
|
| 231 | + (int)sizeof(device->type), device->type,
|
| 232 | + (int)sizeof(model), model,
|
| 233 | + (int)sizeof(vendor), vendor,
|
| 234 | + (int)sizeof(module), module
|
| 235 | );
|
| 236 | }
|
| 237 | - return ret;
|
| 238 | + return -ENOENT;
|
| 239 | }
|
| 240 |
|
| 241 | static DEVICE_ATTR(capabilities, S_IRUGO, ddcci_attr_capabilities_show, NULL);
|
| 242 | @@ -945,33 +892,33 @@ static int ddcci_device_uevent(CSTRUCT device *dev, struct kobj_uevent_env *env)
|
| 243 | ddcci_modalias_clean(vendor, sizeof(vendor), '_');
|
| 244 | ddcci_modalias_clean(module, sizeof(module), '_');
|
| 245 |
|
| 246 | - if (add_uevent_var(env, "MODALIAS=%s%s-%s-%s-%s-%s",
|
| 247 | + if (add_uevent_var(env, "MODALIAS=%s%.*s-%.*s-%.*s-%.*s-%.*s",
|
| 248 | DDCCI_MODULE_PREFIX,
|
| 249 | - device->prot,
|
| 250 | - device->type,
|
| 251 | - model,
|
| 252 | - vendor,
|
| 253 | - module
|
| 254 | + (int)sizeof(device->prot), device->prot,
|
| 255 | + (int)sizeof(device->type), device->type,
|
| 256 | + (int)sizeof(model), model,
|
| 257 | + (int)sizeof(vendor), vendor,
|
| 258 | + (int)sizeof(module), module
|
| 259 | ))
|
| 260 | return -ENOMEM;
|
| 261 |
|
| 262 | if (device->prot[0])
|
| 263 | - if (add_uevent_var(env, "DDCCI_PROT=%s", device->prot))
|
| 264 | + if (add_uevent_var(env, "DDCCI_PROT=%.*s", (int)sizeof(device->prot), device->prot))
|
| 265 | return -ENOMEM;
|
| 266 |
|
| 267 | if (device->type[0])
|
| 268 | - if (add_uevent_var(env, "DDCCI_TYPE=%s", device->type))
|
| 269 | + if (add_uevent_var(env, "DDCCI_TYPE=%.*s", (int)sizeof(device->type), device->type))
|
| 270 | return -ENOMEM;
|
| 271 |
|
| 272 | if (device->model[0])
|
| 273 | - if (add_uevent_var(env, "DDCCI_MODEL=%s", device->model))
|
| 274 | + if (add_uevent_var(env, "DDCCI_MODEL=%.*s", (int)sizeof(device->model), device->model))
|
| 275 | return -ENOMEM;
|
| 276 |
|
| 277 | if (device->vendor[0]) {
|
| 278 | - if (add_uevent_var(env, "DDCCI_VENDOR=%s", device->vendor))
|
| 279 | + if (add_uevent_var(env, "DDCCI_VENDOR=%.*s", (int)sizeof(device->vendor), device->vendor))
|
| 280 | return -ENOMEM;
|
| 281 |
|
| 282 | - if (add_uevent_var(env, "DDCCI_MODULE=%s", device->module))
|
| 283 | + if (add_uevent_var(env, "DDCCI_MODULE=%.*s", (int)sizeof(device->module), device->module))
|
| 284 | return -ENOMEM;
|
| 285 |
|
| 286 | if (add_uevent_var(env, "DDCCI_UNIQ=%d", device->device_number))
|
| 287 | --
|
| 288 | GitLab
|
| 289 |
|