linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: nphamcs@gmail.com
Cc: linux-mm@kvack.org
Subject: [bug report] cachestat: implement cachestat syscall
Date: Fri, 5 May 2023 11:44:41 +0300	[thread overview]
Message-ID: <bd163947-c261-423a-9bc1-e2ce6c05b56c@kili.mountain> (raw)

Hello Nhat Pham,

The patch 5c289a59b1d0: "cachestat: implement cachestat syscall" from
May 2, 2023, leads to the following Smatch static checker warning:

	mm/filemap.c:4282 __do_sys_cachestat()
	warn: potential integer overflow from user (local copy) 'csr.off + csr.len'

mm/filemap.c
  4250  SYSCALL_DEFINE4(cachestat, unsigned int, fd,
  4251                  struct cachestat_range __user *, cstat_range,
  4252                  struct cachestat __user *, cstat, unsigned int, flags)
  4253  {
  4254          struct fd f = fdget(fd);
  4255          struct address_space *mapping;
  4256          struct cachestat_range csr;
  4257          struct cachestat cs;
  4258          pgoff_t first_index, last_index;
  4259  
  4260          if (!f.file)
  4261                  return -EBADF;
  4262  
  4263          if (copy_from_user(&csr, cstat_range,

csr comes from the user.

  4264                          sizeof(struct cachestat_range))) {
  4265                  fdput(f);
  4266                  return -EFAULT;
  4267          }
  4268  
  4269          /* hugetlbfs is not supported */
  4270          if (is_file_hugepages(f.file)) {
  4271                  fdput(f);
  4272                  return -EOPNOTSUPP;
  4273          }
  4274  
  4275          if (flags != 0) {
  4276                  fdput(f);
  4277                  return -EINVAL;
  4278          }
  4279  
  4280          first_index = csr.off >> PAGE_SHIFT;
  4281          last_index =
  4282                  csr.len == 0 ? ULONG_MAX : (csr.off + csr.len - 1) >> PAGE_SHIFT;
                                                    ^^^^^^^^^^^^^^^^^^^^^^
This can integer overflow.   Do we need some checking to ensure that
first_index < last_index?

  4283          memset(&cs, 0, sizeof(struct cachestat));
  4284          mapping = f.file->f_mapping;
  4285          filemap_cachestat(mapping, first_index, last_index, &cs);
  4286          fdput(f);
  4287  
  4288          if (copy_to_user(cstat, &cs, sizeof(struct cachestat)))
  4289                  return -EFAULT;
  4290  
  4291          return 0;
  4292  }

regards,
dan carpenter


             reply	other threads:[~2023-05-05  8:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-05  8:44 Dan Carpenter [this message]
2023-05-05 16:27 ` Nhat Pham
2023-05-05 20:05   ` Dan Carpenter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bd163947-c261-423a-9bc1-e2ce6c05b56c@kili.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox