Skip to contentSkip to conversation

drm/nouveau/device: append a NUL-terminated character for the string which filled by strncpy()

PATCH In review Luo Jiaxing 2 participants 3 messages Feb 25, 2021 – Feb 27, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/nouveau/device: append a NUL-terminated character for the string which filled by strncpy()

Following warning is found when using W=1 to build kernel:

In function ‘nvkm_udevice_info’,
inlined from ‘nvkm_udevice_mthd’ at drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:195:10:
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:164:2: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]
164 | strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:165:2: warning: ‘strncpy’ specified bound 64 equals destination size [-Wstringop-truncation]
165 | strncpy(args->v0.name, device->name, sizeof(args->v0.name));

The reason of this warning is strncpy() does not guarantee that the destination buffer will be NUL terminated. If the length of source string is bigger than number we set by third input parameter, only first [number] of characters is copied to the destination, and no NUL-terminated is automatically added. There are some potential risks.

---
 drivers/gpu/drm/nouveau/nvkm/engine/device/user.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +4 -2
index fea9d8f..2a32fe0 100644
@@ -161,8 +161,10 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size)
 	if (imem && args->v0.ram_size > 0)
 		args->v0.ram_user = args->v0.ram_user - imem->reserved;
 
-	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
-	strncpy(args->v0.name, device->name, sizeof(args->v0.name));
+	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip) - 1);
+	args->v0.chip[sizeof(args->v0.chip) - 1] = '\0';
+	strncpy(args->v0.name, device->name, sizeof(args->v0.name) - 1);
+	args->v0.name[sizeof(args->v0.name) - 1] = '\0';
 	return 0;
 }
 
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 41 lines

-----Original Message-----
From: Luo Jiaxing [mailto:[email protected]]
Sent: Friday, February 26, 2021 12:39 AM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; luojiaxing <[email protected]>
Subject: [Linuxarm] [PATCH v1] drm/nouveau/device: append a NUL-terminated character for the string which filled by strncpy()

Following warning is found when using W=1 to build kernel:

In function ‘nvkm_udevice_info’,
inlined from ‘nvkm_udevice_mthd’ at
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:195:10:
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:164:2: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]
164 | strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:165:2: warning: ‘strncpy’ specified bound 64 equals destination size [-Wstringop-truncation]
165 | strncpy(args->v0.name, device->name, sizeof(args->v0.name));

The reason of this warning is strncpy() does not guarantee that the destination buffer will be NUL terminated. If the length of source string is bigger than number we set by third input parameter, only first [number] of characters is copied to the destination, and no NUL-terminated is automatically added. There are some potential risks.

---
 drivers/gpu/drm/nouveau/nvkm/engine/device/user.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +0 -0
index fea9d8f..2a32fe0 100644

drivers/gpu/drm/nouveau/nvkm/engine/device/user.c nvkm_udevice_info(struct nvkm_udevice *udev, void *data, · line 168

Quoted 10 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +4 -2
@@ -161,8 +161,10 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data,
 u32 size)
 	if (imem && args->v0.ram_size > 0)
 		args->v0.ram_user = args->v0.ram_user - imem->reserved;
 
-	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
-	strncpy(args->v0.name, device->name, sizeof(args->v0.name));
+	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip) - 1);
+	args->v0.chip[sizeof(args->v0.chip) - 1] = '\0';
+	strncpy(args->v0.name, device->name, sizeof(args->v0.name) - 1);
+	args->v0.name[sizeof(args->v0.name) - 1] = '\0';

Isn't it better to use snprintf()?

drivers/gpu/drm/nouveau/nvkm/engine/device/user.c nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size) · line 169

Quoted 3 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +0 -0
 	return 0;
 }

Thanks
Barry

author
#2 lore raw
Quoted 44 lines · Song Bao Hua (Barry Song)
Quoted 44 lines

-----Original Message-----
From: Luo Jiaxing [mailto:[email protected]]
Sent: Friday, February 26, 2021 12:39 AM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; luojiaxing <[email protected]>
Subject: [Linuxarm] [PATCH v1] drm/nouveau/device: append a NUL-terminated character for the string which filled by strncpy()

Following warning is found when using W=1 to build kernel:

In function ‘nvkm_udevice_info’,
inlined from ‘nvkm_udevice_mthd’ at
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:195:10:
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:164:2: warning: ‘strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]
164 | strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:165:2: warning: ‘strncpy’ specified bound 64 equals destination size [-Wstringop-truncation]
165 | strncpy(args->v0.name, device->name, sizeof(args->v0.name));

The reason of this warning is strncpy() does not guarantee that the destination buffer will be NUL terminated. If the length of source string is bigger than number we set by third input parameter, only first [number] of characters is copied to the destination, and no NUL-terminated is automatically added. There are some potential risks.

---
  drivers/gpu/drm/nouveau/nvkm/engine/device/user.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +0 -0
index fea9d8f..2a32fe0 100644
@@ -161,8 +161,10 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data,
 u32 size)
  	if (imem && args->v0.ram_size > 0)
  		args->v0.ram_user = args->v0.ram_user - imem->reserved;
 
Quoted 8 lines

drivers/gpu/drm/nouveau/nvkm/engine/device/user.c nvkm_udevice_info(struct nvkm_udevice *udev, void *data, · line 168

Quoted 6 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +4 -2
-	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip));
-	strncpy(args->v0.name, device->name, sizeof(args->v0.name));
+	strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip) - 1);
+	args->v0.chip[sizeof(args->v0.chip) - 1] = '\0';
+	strncpy(args->v0.name, device->name, sizeof(args->v0.name) - 1);
+	args->v0.name[sizeof(args->v0.name) - 1] = '\0';

Isn't it better to use snprintf()?

yes, you are right,  snprintf() is better. Most of drivers use snprintf() to format a string,

but still some examples in kernel that use it for copy.

I modify to code to the follow and I think it's the same with strncpy but more safety

drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +0 -0
index fea9d8f..4bf65bb 100644
@@ -161,8 +161,8 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size)

        if (imem && args->v0.ram_size > 0)
                args->v0.ram_user = args->v0.ram_user - imem->reserved;

-       strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); -       strncpy(args->v0.name, device->name, sizeof(args->v0.name)); +       snprintf(args->v0.chip, sizeof(args->v0.chip), "%s", device->chip->name); +       snprintf(args->v0.name, sizeof(args->v0.name), "%s", device->name);

Thanks

Jiaxing

Quoted 7 lines

drivers/gpu/drm/nouveau/nvkm/engine/device/user.c nvkm_udevice_info(struct nvkm_udevice *udev, void *data, · line 170

Quoted 4 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +0 -0
  	return 0;
  }

Thanks
Barry

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel