* [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy
@ 2021-02-22 15:12 Romain Perier
2021-02-22 15:12 ` [PATCH 01/20] cgroup: Manual replacement of the deprecated strlcpy() with return values Romain Perier
[not found] ` <20210222151231.22572-1-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
0 siblings, 2 replies; 6+ messages in thread
From: Romain Perier @ 2021-02-22 15:12 UTC (permalink / raw)
To: Kees Cook, kernel-hardening, Tejun Heo, Zefan Li, Johannes Weiner,
Herbert Xu, David S. Miller, Jiri Pirko, Sumit Semwal,
Christian König, Greg Kroah-Hartman, Mimi Zohar,
Dmitry Kasatkin, J. Bruce Fields, Chuck Lever, Geert Uytterhoeven,
Jessica Yu, Guenter Roeck, Heiko Carstens, Vasily Gorbik,
Christian Borntraeger, Steffen Maier
Cc: Romain Perier, cgroups, linux-crypto, netdev, linux-media,
dri-devel, linaro-mm-sig, Rafael J. Wysocki, linux-integrity,
linux-nfs, linux-m68k, linux-hwmon, linux-s390, linux-scsi,
target-devel, alsa-devel, linux-usb, linux-watchdog, linux-kernel
strlcpy() copy a C-String into a sized buffer, the result is always a
valid NULL-terminated that fits in the buffer, howerver it has severals
issues. It reads the source buffer first, which is dangerous if it is non
NULL-terminated or if the corresponding buffer is unbounded. Its safe
replacement is strscpy(), as suggested in the deprecated interface [1].
We plan to make this contribution in two steps:
- Firsly all cases of strlcpy's return value are manually replaced by the
corresponding calls of strscpy() with the new handling of the return
value (as the return code is different in case of error).
- Then all other cases are automatically replaced by using coccinelle.
This series covers manual replacements.
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
Romain Perier (20):
cgroup: Manual replacement of the deprecated strlcpy() with return
values
crypto: Manual replacement of the deprecated strlcpy() with return
values
devlink: Manual replacement of the deprecated strlcpy() with return
values
dma-buf: Manual replacement of the deprecated strlcpy() with return
values
kobject: Manual replacement of the deprecated strlcpy() with return
values
ima: Manual replacement of the deprecated strlcpy() with return values
SUNRPC: Manual replacement of the deprecated strlcpy() with return
values
kernfs: Manual replacement of the deprecated strlcpy() with return
values
m68k/atari: Manual replacement of the deprecated strlcpy() with return
values
module: Manual replacement of the deprecated strlcpy() with return
values
hwmon: Manual replacement of the deprecated strlcpy() with return
values
s390/hmcdrv: Manual replacement of the deprecated strlcpy() with
return values
scsi: zfcp: Manual replacement of the deprecated strlcpy() with return
values
target: Manual replacement of the deprecated strlcpy() with return
values
ALSA: usb-audio: Manual replacement of the deprecated strlcpy() with
return values
tracing/probe: Manual replacement of the deprecated strlcpy() with
return values
vt: Manual replacement of the deprecated strlcpy() with return values
usb: gadget: f_midi: Manual replacement of the deprecated strlcpy()
with return values
usbip: usbip_host: Manual replacement of the deprecated strlcpy() with
return values
s390/watchdog: Manual replacement of the deprecated strlcpy() with
return values
arch/m68k/emu/natfeat.c | 6 +--
crypto/lrw.c | 6 +--
crypto/xts.c | 6 +--
drivers/dma-buf/dma-buf.c | 4 +-
drivers/hwmon/pmbus/max20730.c | 66 +++++++++++++------------
drivers/s390/char/diag_ftp.c | 4 +-
drivers/s390/char/sclp_ftp.c | 6 +--
drivers/s390/scsi/zfcp_fc.c | 8 +--
drivers/target/target_core_configfs.c | 33 ++++---------
drivers/tty/vt/keyboard.c | 5 +-
drivers/usb/gadget/function/f_midi.c | 4 +-
drivers/usb/gadget/function/f_printer.c | 8 +--
drivers/usb/usbip/stub_main.c | 6 +--
drivers/watchdog/diag288_wdt.c | 12 +++--
fs/kernfs/dir.c | 27 +++++-----
kernel/cgroup/cgroup.c | 2 +-
kernel/module.c | 4 +-
kernel/trace/trace_uprobe.c | 11 ++---
lib/kobject_uevent.c | 6 +--
net/core/devlink.c | 6 +--
net/sunrpc/clnt.c | 6 ++-
security/integrity/ima/ima_policy.c | 8 ++-
sound/usb/card.c | 4 +-
23 files changed, 129 insertions(+), 119 deletions(-)
--
2.20.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 01/20] cgroup: Manual replacement of the deprecated strlcpy() with return values
2021-02-22 15:12 [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy Romain Perier
@ 2021-02-22 15:12 ` Romain Perier
[not found] ` <20210222151231.22572-2-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
[not found] ` <20210222151231.22572-1-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
1 sibling, 1 reply; 6+ messages in thread
From: Romain Perier @ 2021-02-22 15:12 UTC (permalink / raw)
To: Kees Cook, kernel-hardening, Tejun Heo, Zefan Li, Johannes Weiner
Cc: Romain Perier, cgroups, linux-kernel
The strlcpy() reads the entire source buffer first, it is dangerous if
the source buffer lenght is unbounded or possibility non NULL-terminated.
It can lead to linear read overflows, crashes, etc...
As recommended in the deprecated interfaces [1], it should be replaced
by strscpy.
This commit replaces all calls to strlcpy that handle the return values
by the corresponding strscpy calls with new handling of the return
values (as it is quite different between the two functions).
[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
Signed-off-by: Romain Perier <romain.perier@gmail.com>
---
kernel/cgroup/cgroup.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 1ea995f801ec..bac0dc2ff8ad 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2265,7 +2265,7 @@ int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
} else {
/* if no hierarchy exists, everyone is in "/" */
- ret = strlcpy(buf, "/", buflen);
+ ret = strscpy(buf, "/", buflen);
}
spin_unlock_irq(&css_set_lock);
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy
[not found] ` <20210222151231.22572-1-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2021-02-22 16:36 ` Shuah Khan
2021-02-23 9:31 ` Romain Perier
0 siblings, 1 reply; 6+ messages in thread
From: Shuah Khan @ 2021-02-22 16:36 UTC (permalink / raw)
To: Romain Perier, Kees Cook,
kernel-hardening-ZwoEplunGu1jrUoiu81ncdBPR1lH4CV8, Tejun Heo,
Zefan Li, Johannes Weiner, Herbert Xu, David S. Miller,
Jiri Pirko, Sumit Semwal, Christian König,
Greg Kroah-Hartman, Mimi Zohar, Dmitry Kasatkin, J. Bruce Fields,
Chuck Lever, Geert Uytterhoeven, Jessica Yu, Guenter Roeck
Cc: cgroups-u79uwXL29TY76Z2rM5mHXA,
linux-crypto-u79uwXL29TY76Z2rM5mHXA,
netdev-u79uwXL29TY76Z2rM5mHXA, linux-media-u79uwXL29TY76Z2rM5mHXA,
dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW,
linaro-mm-sig-cunTk1MwBs8s++Sfvej+rw, Rafael J. Wysocki,
linux-integrity-u79uwXL29TY76Z2rM5mHXA,
linux-nfs-u79uwXL29TY76Z2rM5mHXA,
linux-m68k-cunTk1MwBs8S/qaLPR03pWD2FQJk+8+b,
linux-hwmon-u79uwXL29TY76Z2rM5mHXA,
linux-s390-u79uwXL29TY76Z2rM5mHXA,
linux-scsi-u79uwXL29TY76Z2rM5mHXA,
target-devel-u79uwXL29TY76Z2rM5mHXA,
alsa-devel-K7yf7f+aM1XWsZ/bQMPhNw,
linux-usb-u79uwXL29TY76Z2rM5mHXA,
linux-watchdog-u79uwXL29TY76Z2rM5mHXA,
linux-kernel-u79uwXL29TY76Z2rM5mHXA, Shuah Khan
On 2/22/21 8:12 AM, Romain Perier wrote:
> strlcpy() copy a C-String into a sized buffer, the result is always a
> valid NULL-terminated that fits in the buffer, howerver it has severals
> issues. It reads the source buffer first, which is dangerous if it is non
> NULL-terminated or if the corresponding buffer is unbounded. Its safe
> replacement is strscpy(), as suggested in the deprecated interface [1].
>
> We plan to make this contribution in two steps:
> - Firsly all cases of strlcpy's return value are manually replaced by the
> corresponding calls of strscpy() with the new handling of the return
> value (as the return code is different in case of error).
> - Then all other cases are automatically replaced by using coccinelle.
>
Cool. A quick check shows me 1031 strscpy() calls with no return
checks. All or some of these probably need to be reviewed and add
return checks. Is this something that is in the plan to address as
part of this work?
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy
2021-02-22 16:36 ` [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy Shuah Khan
@ 2021-02-23 9:31 ` Romain Perier
0 siblings, 0 replies; 6+ messages in thread
From: Romain Perier @ 2021-02-23 9:31 UTC (permalink / raw)
To: Shuah Khan
Cc: Kees Cook, Kernel Hardening, Tejun Heo, Zefan Li, Johannes Weiner,
Herbert Xu, David S. Miller, Jiri Pirko, Sumit Semwal,
Christian König, Greg Kroah-Hartman, Mimi Zohar,
Dmitry Kasatkin, J. Bruce Fields, Chuck Lever, Geert Uytterhoeven,
Jessica Yu, Guenter Roeck, Heiko Carstens, Vasily Gorbik,
Christian Borntraeger, Steffen Maier
[-- Attachment #1: Type: text/plain, Size: 888 bytes --]
Le lun. 22 févr. 2021 à 17:36, Shuah Khan <skhan@linuxfoundation.org> a
écrit :
>
> Cool. A quick check shows me 1031 strscpy() calls with no return
> checks. All or some of these probably need to be reviewed and add
> return checks. Is this something that is in the plan to address as
> part of this work?
>
> thanks,
> -- Shuah
>
Hi,
Initially, what we planned with Kees is to firstly replace all calls with
error handling codes (like this series does),
and then replace all other simple calls (without error handling). However,
we can also start a discussion about this topic, all suggestions are
welcome.
I am not sure that it does make sense to check all returns code in all
cases (for example in arch/alpha/kernel/setup.c, there are a ton of other
examples in the kernel). But a general review (as you suggest), would make
sense.
Regards,
Romain
[-- Attachment #2: Type: text/html, Size: 1312 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 01/20] cgroup: Manual replacement of the deprecated strlcpy() with return values
[not found] ` <20210222151231.22572-2-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2021-02-23 16:13 ` Michal Koutný
2021-02-23 17:49 ` Romain Perier
0 siblings, 1 reply; 6+ messages in thread
From: Michal Koutný @ 2021-02-23 16:13 UTC (permalink / raw)
To: Romain Perier
Cc: Kees Cook, kernel-hardening-ZwoEplunGu1jrUoiu81ncdBPR1lH4CV8,
Tejun Heo, Zefan Li, Johannes Weiner,
cgroups-u79uwXL29TY76Z2rM5mHXA,
linux-kernel-u79uwXL29TY76Z2rM5mHXA
[-- Attachment #1: Type: text/plain, Size: 3425 bytes --]
Hello.
On Mon, Feb 22, 2021 at 04:12:12PM +0100, Romain Perier <romain.perier@gmail.com> wrote:
> --- a/kernel/cgroup/cgroup.c
> +++ b/kernel/cgroup/cgroup.c
> @@ -2265,7 +2265,7 @@ int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Actually, this function isn't used at all. So I'd instead propose the
patch below.
-- >8 --
From 4f7e0b9c0412f60e0b0e8b7d1ef6eb2790dca567 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Koutn=C3=BD?= <mkoutny-IBi9RG/b67k@public.gmane.org>
Date: Tue, 23 Feb 2021 17:05:57 +0100
Subject: [PATCH] cgroup: Drop task_cgroup_path()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The function has no current users and it is a remnant from kdbus
enthusiasm era 857a2beb09ab ("cgroup: implement
task_cgroup_path_from_hierarchy()"). Drop it to eliminate unused code.
Suggested-by: Romain Perier <romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Signed-off-by: Michal Koutný <mkoutny-IBi9RG/b67k@public.gmane.org>
---
include/linux/cgroup.h | 1 -
kernel/cgroup/cgroup.c | 39 ---------------------------------------
2 files changed, 40 deletions(-)
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 4f2f79de083e..e9c41b15fd4e 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -115,7 +115,6 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
int cgroup_rm_cftypes(struct cftype *cfts);
void cgroup_file_notify(struct cgroup_file *cfile);
-int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
struct pid *pid, struct task_struct *tsk);
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index c80fe99f85ae..d75ffd461222 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2235,45 +2235,6 @@ int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
}
EXPORT_SYMBOL_GPL(cgroup_path_ns);
-/**
- * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
- * @task: target task
- * @buf: the buffer to write the path into
- * @buflen: the length of the buffer
- *
- * Determine @task's cgroup on the first (the one with the lowest non-zero
- * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
- * function grabs cgroup_mutex and shouldn't be used inside locks used by
- * cgroup controller callbacks.
- *
- * Return value is the same as kernfs_path().
- */
-int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
-{
- struct cgroup_root *root;
- struct cgroup *cgrp;
- int hierarchy_id = 1;
- int ret;
-
- mutex_lock(&cgroup_mutex);
- spin_lock_irq(&css_set_lock);
-
- root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
-
- if (root) {
- cgrp = task_cgroup_from_root(task, root);
- ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
- } else {
- /* if no hierarchy exists, everyone is in "/" */
- ret = strlcpy(buf, "/", buflen);
- }
-
- spin_unlock_irq(&css_set_lock);
- mutex_unlock(&cgroup_mutex);
- return ret;
-}
-EXPORT_SYMBOL_GPL(task_cgroup_path);
-
/**
* cgroup_migrate_add_task - add a migration target task to a migration context
* @task: target task
--
2.30.1
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 01/20] cgroup: Manual replacement of the deprecated strlcpy() with return values
2021-02-23 16:13 ` Michal Koutný
@ 2021-02-23 17:49 ` Romain Perier
0 siblings, 0 replies; 6+ messages in thread
From: Romain Perier @ 2021-02-23 17:49 UTC (permalink / raw)
To: Michal Koutný
Cc: Kees Cook, Kernel Hardening, Tejun Heo, Zefan Li, Johannes Weiner,
cgroups, Linux Kernel Mailing List
[-- Attachment #1: Type: text/plain, Size: 500 bytes --]
Le mar. 23 févr. 2021 à 17:13, Michal Koutný <mkoutny@suse.com> a écrit :
> Hello.
>
> On Mon, Feb 22, 2021 at 04:12:12PM +0100, Romain Perier <
> romain.perier@gmail.com> wrote:
> > --- a/kernel/cgroup/cgroup.c
> > +++ b/kernel/cgroup/cgroup.c
> > @@ -2265,7 +2265,7 @@ int task_cgroup_path(struct task_struct *task,
> char *buf, size_t buflen)
> Actually, this function isn't used at all. So I'd instead propose the
> patch below.
>
+1 if it is dead code.
Thanks,
Romain
[-- Attachment #2: Type: text/html, Size: 932 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-02-23 17:49 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-22 15:12 [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy Romain Perier
2021-02-22 15:12 ` [PATCH 01/20] cgroup: Manual replacement of the deprecated strlcpy() with return values Romain Perier
[not found] ` <20210222151231.22572-2-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-02-23 16:13 ` Michal Koutný
2021-02-23 17:49 ` Romain Perier
[not found] ` <20210222151231.22572-1-romain.perier-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2021-02-22 16:36 ` [PATCH 00/20] Manual replacement of all strlcpy in favor of strscpy Shuah Khan
2021-02-23 9:31 ` Romain Perier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox