public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Adam Zabrocki <pi3@pi3.com.pl>
To: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>,
	Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
	"David S. Miller" <davem@davemloft.net>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	linux-kernel@vger.kernel.org, Solar Designer <solar@openwall.com>
Subject: KRETPROBES are broken since the commit 73f9b911faa7
Date: Fri, 15 Apr 2022 20:07:23 +0200	[thread overview]
Message-ID: <20220415180723.GA13921@pi3.com.pl> (raw)

Hello,

The recent KRETPROBE kernel changes introduced a potential NULL pointer
dereference bug when return handler is not set. The root of the problem
are the following commits related to "kprobes: Use rethook for kretprobe
if possible":
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f3a112c0c40dd96d53c8bdf3ea8d94d528f3b7b8
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=73f9b911faa74ac5107879de05c9489c419f41bb


When user sets up a KRETPROBE, kernel eventually ends up here:

static void kretprobe_rethook_handler(struct rethook_node *rh, void *data,
                      struct pt_regs *regs)
{
    struct kretprobe *rp = (struct kretprobe *)data;
    struct kretprobe_instance *ri;
    struct kprobe_ctlblk *kcb;

    /* The data must NOT be null. This means rethook data structure is broken. */
    if (WARN_ON_ONCE(!data))
        return;

    __this_cpu_write(current_kprobe, &rp->kp);
    kcb = get_kprobe_ctlblk();
    kcb->kprobe_status = KPROBE_HIT_ACTIVE;

    ri = container_of(rh, struct kretprobe_instance, node);
    rp->handler(ri, regs);

    __this_cpu_write(current_kprobe, NULL);
}
NOKPROBE_SYMBOL(kretprobe_rethook_handler);

Unfortunately, rp->handler is not verified against NULL. If user does not
set up a return handler, KRETPROBE shouldn't call it (that's the defined
behavior in the documentation, quoting "Any or all handlers can be NULL").
However, kretprobe_rethook_handler function does not enforce such behavior.

I'm going to send a patch for it in a reply to this message.

Thanks,
Adam

-- 
pi3 (pi3ki31ny) - pi3 (at) itsec pl
http://pi3.com.pl


             reply	other threads:[~2022-04-15 18:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-15 18:07 Adam Zabrocki [this message]
2022-04-15 18:10 ` [PATCH] x86/kprobes: Fix KRETPROBES when CONFIG_KRETPROBE_ON_RETHOOK is set Adam Zabrocki
2022-04-16 14:06   ` Masami Hiramatsu
2022-04-22 13:05   ` Masami Hiramatsu
2022-04-22 13:43     ` Steven Rostedt
2022-04-22 14:54       ` Masami Hiramatsu
2022-04-22 16:35         ` Adam Zabrocki

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=20220415180723.GA13921@pi3.com.pl \
    --to=pi3@pi3.com.pl \
    --cc=anil.s.keshavamurthy@intel.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=naveen.n.rao@linux.ibm.com \
    --cc=solar@openwall.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