[ammarfaizi2-block:tglx/devel/printk 16/29] kernel/printk/printk.c:759 devkmsg_read() warn: variable dereferenced before check 'user' (see line 754)

From: Dan Carpenter
Date: Tue Sep 13 2022 - 05:16:30 EST


tree: https://github.com/ammarfaizi2/linux-block tglx/devel/printk
head: f6339cd6dc7f812f822e86bd78d1edbf2bfc730b
commit: 4984141cc2064cdf4e4a122fa0d895bd93943e4e [16/29] printk: Use struct cons_text_buf
config: x86_64-randconfig-m001
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>

smatch warnings:
kernel/printk/printk.c:759 devkmsg_read() warn: variable dereferenced before check 'user' (see line 754)

vim +/user +759 kernel/printk/printk.c

e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 749 static ssize_t devkmsg_read(struct file *file, char __user *buf,
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 750 size_t count, loff_t *ppos)
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 751 {
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 752 struct devkmsg_user *user = file->private_data;
896fbe20b4e233 kernel/printk/printk.c John Ogness 2020-07-09 753 struct printk_record *r = &user->record;
4984141cc2064c kernel/printk/printk.c Thomas Gleixner 2022-09-03 @754 char *outbuf = user->txtbuf.ext_text;
^^^^^^
Dereference

4984141cc2064c kernel/printk/printk.c Thomas Gleixner 2022-09-03 755 const int maxlen = sizeof(user->txtbuf.ext_text);
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 756 size_t len;
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 757 ssize_t ret;
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 758
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 @759 if (!user)
^^^^^
Checked too late.

e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 760 return -EBADF;
e11fea92e13fb9 kernel/printk.c Kay Sievers 2012-05-03 761
4a77a5a06ec66e kernel/printk.c Yuanhan Liu 2012-06-16 762 ret = mutex_lock_interruptible(&user->lock);
4a77a5a06ec66e kernel/printk.c Yuanhan Liu 2012-06-16 763 if (ret)
4a77a5a06ec66e kernel/printk.c Yuanhan Liu 2012-06-16 764 return ret;
de6fcbdb68b29b kernel/printk/printk.c Sergey Senozhatsky 2016-12-27 765