linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: ziy@nvidia.com
Cc: linux-mm@kvack.org
Subject: [bug report] mm: huge_memory: enable debugfs to split huge pages to any order
Date: Thu, 7 Mar 2024 16:49:37 +0300	[thread overview]
Message-ID: <7dda9283-b437-4cf8-ab0d-83c330deb9c0@moroto.mountain> (raw)

Hello Zi Yan,

Commit fc4d182316bd ("mm: huge_memory: enable debugfs to split huge
pages to any order") from Feb 26, 2024 (linux-next), leads to the
following Smatch static checker warning:

	mm/huge_memory.c:2898 __split_huge_page()
	error: undefined (user controlled) shift '1 << new_order'

mm/huge_memory.c
    2889 static void __split_huge_page(struct page *page, struct list_head *list,
    2890                 pgoff_t end, unsigned int new_order)
    2891 {
    2892         struct folio *folio = page_folio(page);
    2893         struct page *head = &folio->page;
    2894         struct lruvec *lruvec;
    2895         struct address_space *swap_cache = NULL;
    2896         unsigned long offset = 0;
    2897         int i, nr_dropped = 0;
--> 2898         unsigned int new_nr = 1 << new_order;
                                            ^^^^^^^^^
The new_order variable comes from the user via debugfs.

    2899         int order = folio_order(folio);
    2900         unsigned int nr = 1 << order;
    2901 
    2902         /* complete memcg works before add pages to LRU */
    2903         split_page_memcg(head, order, new_order);
    2904 
    2905         if (folio_test_anon(folio) && folio_test_swapcache(folio)) {
    2906                 offset = swp_offset(folio->swap);
    2907                 swap_cache = swap_address_space(folio->swap);

Here is the debugfs code in split_huge_pages_write()

mm/huge_memory.c
  3628  
  3629          ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d", &pid, &vaddr_start, &vaddr_end, &new_order);
                                                                                             ^^^^^^^^^^
We just read new_order

  3630          if (ret == 1 && pid == 1) {
  3631                  split_huge_pages_all();
  3632                  ret = strlen(input_buf);
  3633                  goto out;
  3634          } else if (ret != 3 && ret != 4) {
  3635                  ret = -EINVAL;
  3636                  goto out;
  3637          }
  3638  
  3639          ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order);
                                                                        ^^^^^^^^^
And pass it directly with no bounds checking.  Debugfs code is root
only...  We used to take a view that if root does something stupid then
they get what they deserve.  But these days syzbot is fuzz testing stuff
even when it's root only and complaining about shift wraps or other
undefined behavior.  So I feel like it might be easiest to silence this
undefined behavior warning now instead of waiting for the syzbot reports
to come back to bite us in a couple years.

  3640          if (!ret)
  3641                  ret = strlen(input_buf);
  3642  out:
  3643          mutex_unlock(&split_debug_mutex);
  3644          return ret;
  3645  
  3646  }




regards,
dan carpenter


             reply	other threads:[~2024-03-07 13:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07 13:49 Dan Carpenter [this message]
2024-03-07 14:20 ` [bug report] mm: huge_memory: enable debugfs to split huge pages to any order Zi Yan
2024-03-07 14:31   ` Dan Carpenter
2024-03-07 14:41     ` Zi Yan
2024-03-07 14:44       ` 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=7dda9283-b437-4cf8-ab0d-83c330deb9c0@moroto.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=linux-mm@kvack.org \
    --cc=ziy@nvidia.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;
as well as URLs for NNTP newsgroup(s).