2021-12-17 07:49:36

by Dan Carpenter

[permalink] [raw]
Subject: [axboe-block:for-5.17/block 106/108] block/blk-ioc.c:307 set_task_ioprio() error: we previously assumed 'task->io_context' could be null (see line 300)

tree: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-5.17/block
head: 5ef1630586317e92c9ebd7b4ce48f393b7ff790f
commit: 5fc11eebb4a98df5324a4de369bb5ab7f0007ff7 [106/108] block: open code create_task_io_context in set_task_ioprio
config: i386-randconfig-m021-20211216 (https://download.01.org/0day-ci/archive/20211217/[email protected]/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>
Reported-by: Dan Carpenter <[email protected]>

smatch warnings:
block/blk-ioc.c:307 set_task_ioprio() error: we previously assumed 'task->io_context' could be null (see line 300)

vim +307 block/blk-ioc.c

a411cd3cfdc5bb Christoph Hellwig 2021-12-09 271 int set_task_ioprio(struct task_struct *task, int ioprio)
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 272 {
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 273 int err;
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 274 const struct cred *cred = current_cred(), *tcred;
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 275
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 276 rcu_read_lock();
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 277 tcred = __task_cred(task);
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 278 if (!uid_eq(tcred->uid, cred->euid) &&
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 279 !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 280 rcu_read_unlock();
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 281 return -EPERM;
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 282 }
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 283 rcu_read_unlock();
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 284
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 285 err = security_task_setioprio(task, ioprio);
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 286 if (err)
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 287 return err;
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 288
8472161b77c41d Christoph Hellwig 2021-12-09 289 task_lock(task);
8472161b77c41d Christoph Hellwig 2021-12-09 290 if (unlikely(!task->io_context)) {
8472161b77c41d Christoph Hellwig 2021-12-09 291 struct io_context *ioc;
8472161b77c41d Christoph Hellwig 2021-12-09 292
8472161b77c41d Christoph Hellwig 2021-12-09 293 task_unlock(task);
5fc11eebb4a98d Christoph Hellwig 2021-12-09 294
5fc11eebb4a98d Christoph Hellwig 2021-12-09 295 ioc = alloc_io_context(GFP_ATOMIC, NUMA_NO_NODE);
5fc11eebb4a98d Christoph Hellwig 2021-12-09 296 if (!ioc)
5fc11eebb4a98d Christoph Hellwig 2021-12-09 297 return -ENOMEM;
5fc11eebb4a98d Christoph Hellwig 2021-12-09 298
5fc11eebb4a98d Christoph Hellwig 2021-12-09 299 task_lock(task);
5fc11eebb4a98d Christoph Hellwig 2021-12-09 @300 if (task->io_context || (task->flags & PF_EXITING)) {
^^^^^^^^^^^^^^^^^^^^^^^^
Assume "task->io_context" is NULL but PF_EXITING is set.

5fc11eebb4a98d Christoph Hellwig 2021-12-09 301 kmem_cache_free(iocontext_cachep, ioc);
5fc11eebb4a98d Christoph Hellwig 2021-12-09 302 ioc = task->io_context;
5fc11eebb4a98d Christoph Hellwig 2021-12-09 303 } else {
5fc11eebb4a98d Christoph Hellwig 2021-12-09 304 task->io_context = ioc;
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 305 }
8472161b77c41d Christoph Hellwig 2021-12-09 306 }
8472161b77c41d Christoph Hellwig 2021-12-09 @307 task->io_context->ioprio = ioprio;
^^^^^^^^^^^^^^^^^^
Unchecked dereference.

8472161b77c41d Christoph Hellwig 2021-12-09 308 task_unlock(task);
8472161b77c41d Christoph Hellwig 2021-12-09 309 return 0;
a411cd3cfdc5bb Christoph Hellwig 2021-12-09 310 }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]



2021-12-21 02:57:42

by Eric Dumazet

[permalink] [raw]
Subject: Re: [axboe-block:for-5.17/block 106/108] block/blk-ioc.c:307 set_task_ioprio() error: we previously assumed 'task->io_context' could be null (see line 300)


On 12/16/21 11:49 PM, Dan Carpenter wrote:
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-5.17/block
> head: 5ef1630586317e92c9ebd7b4ce48f393b7ff790f
> commit: 5fc11eebb4a98df5324a4de369bb5ab7f0007ff7 [106/108] block: open code create_task_io_context in set_task_ioprio
> config: i386-randconfig-m021-20211216 (https://download.01.org/0day-ci/archive/20211217/[email protected]/config)
> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <[email protected]>
> Reported-by: Dan Carpenter <[email protected]>
>
> smatch warnings:
> block/blk-ioc.c:307 set_task_ioprio() error: we previously assumed 'task->io_context' could be null (see line 300)
>
> vim +307 block/blk-ioc.c
>
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 271 int set_task_ioprio(struct task_struct *task, int ioprio)
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 272 {
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 273 int err;
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 274 const struct cred *cred = current_cred(), *tcred;
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 275
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 276 rcu_read_lock();
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 277 tcred = __task_cred(task);
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 278 if (!uid_eq(tcred->uid, cred->euid) &&
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 279 !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 280 rcu_read_unlock();
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 281 return -EPERM;
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 282 }
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 283 rcu_read_unlock();
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 284
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 285 err = security_task_setioprio(task, ioprio);
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 286 if (err)
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 287 return err;
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 288
> 8472161b77c41d Christoph Hellwig 2021-12-09 289 task_lock(task);
> 8472161b77c41d Christoph Hellwig 2021-12-09 290 if (unlikely(!task->io_context)) {
> 8472161b77c41d Christoph Hellwig 2021-12-09 291 struct io_context *ioc;
> 8472161b77c41d Christoph Hellwig 2021-12-09 292
> 8472161b77c41d Christoph Hellwig 2021-12-09 293 task_unlock(task);
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 294
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 295 ioc = alloc_io_context(GFP_ATOMIC, NUMA_NO_NODE);
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 296 if (!ioc)
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 297 return -ENOMEM;
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 298
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 299 task_lock(task);
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 @300 if (task->io_context || (task->flags & PF_EXITING)) {
> ^^^^^^^^^^^^^^^^^^^^^^^^
> Assume "task->io_context" is NULL but PF_EXITING is set.
>
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 301 kmem_cache_free(iocontext_cachep, ioc);
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 302 ioc = task->io_context;
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 303 } else {
> 5fc11eebb4a98d Christoph Hellwig 2021-12-09 304 task->io_context = ioc;
> a411cd3cfdc5bb Christoph Hellwig 2021-12-09 305 }
> 8472161b77c41d Christoph Hellwig 2021-12-09 306 }
> 8472161b77c41d Christoph Hellwig 2021-12-09 @307 task->io_context->ioprio = ioprio;
> ^^^^^^^^^^^^^^^^^^
> Unchecked dereference.


I will release a syzbot report with a repro like this:

  syscall(__NR_clone, 0x3a3dd4008400af01ul, 0ul, 0x9999999999999999ul, 0ul,
          -1ul);
  syscall(__NR_ioprio_set, 3ul, 0, 0ul);





2021-12-21 03:31:59

by Jens Axboe

[permalink] [raw]
Subject: Re: [axboe-block:for-5.17/block 106/108] block/blk-ioc.c:307 set_task_ioprio() error: we previously assumed 'task->io_context' could be null (see line 300)

On 12/17/21 12:49 AM, Dan Carpenter wrote:
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-5.17/block
> head: 5ef1630586317e92c9ebd7b4ce48f393b7ff790f
> commit: 5fc11eebb4a98df5324a4de369bb5ab7f0007ff7 [106/108] block: open code create_task_io_context in set_task_ioprio
> config: i386-randconfig-m021-20211216 (https://download.01.org/0day-ci/archive/20211217/[email protected]/config)
> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <[email protected]>
> Reported-by: Dan Carpenter <[email protected]>
>
> smatch warnings:
> block/blk-ioc.c:307 set_task_ioprio() error: we previously assumed 'task->io_context' could be null (see line 300)

This should fix it:


diff --git a/block/blk-ioc.c b/block/blk-ioc.c
index 87bdc9ca8295..71c3a933cf16 100644
--- a/block/blk-ioc.c
+++ b/block/blk-ioc.c
@@ -279,7 +279,12 @@ int set_task_ioprio(struct task_struct *task, int ioprio)
return -ENOMEM;

task_lock(task);
- if (task->io_context || (task->flags & PF_EXITING)) {
+ if (task->flags & PF_EXITING) {
+ err = -ESRCH;
+ kmem_cache_free(iocontext_cachep, ioc);
+ goto out;
+ }
+ if (task->io_context) {
kmem_cache_free(iocontext_cachep, ioc);
ioc = task->io_context;
} else {
@@ -287,8 +292,9 @@ int set_task_ioprio(struct task_struct *task, int ioprio)
}
}
task->io_context->ioprio = ioprio;
+out:
task_unlock(task);
- return 0;
+ return err;
}
EXPORT_SYMBOL_GPL(set_task_ioprio);


--
Jens Axboe