linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-fsdevel <linux-fsdevel@vger.kernel.org>,
	Stephane Eranian <eranian@google.com>
Subject: perfmon trouble
Date: Mon, 11 Jun 2018 03:10:29 +0100	[thread overview]
Message-ID: <20180611021028.GT30522@ZenIV.linux.org.uk> (raw)
In-Reply-To: <20180609155107.GH30522@ZenIV.linux.org.uk>

On Sat, Jun 09, 2018 at 04:51:08PM +0100, Al Viro wrote:

> Stephane, could you comment on the situation in there?  I realize that you
> hadn't touched that thing in more than a decade, but I've no idea who else
> might be familiar with that thing and it's very inconveniently special...

Having looked through that code... ouch.  It tries to have munmap-on-close,
of all things.  Which has interesting consequences; consider, for example,
	fd = perfctl(-1, PFM_CREATE_CONTEXT, &blah, 1);	// create a context
	....
	pid = fork();
	if (!pid) {
		execve("/usr/bin/something_suid", ...);
		...
	}

with something_suid(8) doing an explicit "close each descriptor past stdout"
loop.

PFM_CREATE_CONTEXT has created a context, mmapped its buffer (and stored
the address of that mapping in ctx->ctx_smpl_vaddr) and, having opened
an associated file, sticks it into descriptor table and returns the descriptor.

On fork/exec we have
	* descriptor table copied to child
	* all mappings copied to child and then destroyed by execve
	* execve ends up with the new binary (and libraries, etc.) mmapped
(in child)

Now, our careful suid-root binary does close(2) on its copy of descriptor.
pfm_flush() is called.  ctx->task != current, so we proceed to
        /*
         * remove virtual mapping, if any, for the calling task.
         * cannot reset ctx field until last user is calling close().
         *
         * ctx_smpl_vaddr must never be cleared because it is needed
         * by every task with access to the context
         *
         * When called from do_exit(), the mm context is gone already, therefore
         * mm is NULL, i.e., the VMA is already gone  and we do not have to
         * do anything here
         */
        if (ctx->ctx_smpl_vaddr && current->mm) {
                smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
                smpl_buf_size  = ctx->ctx_smpl_size;
        }

        UNPROTECT_CTX(ctx, flags);

        /*
         * if there was a mapping, then we systematically remove it
         * at this point. Cannot be done inside critical section
         * because some VM function reenables interrupts.
         *
         */
        if (smpl_buf_vaddr) pfm_remove_smpl_mapping(smpl_buf_vaddr, smpl_buf_size);

... with the last call doing vm_munmap() on the area in question.  In the
address space of that suid-root binary, taking out whatever *it* had mapped
at that address range...

I wouldn't be surprised if that turned out to be realistically exploitable ;-/
Is there any documentation of that thing's semantics?  perfmonctl(2) doesn't
mention the mapping at all and link to HP site in the arch/ia64/kernel/perfmon.c
is 404-compliant.  Playing with archive.org brings a sourceforget reference,
but I hadn't been able to find anything ia64-related docs in there...

  reply	other threads:[~2018-06-11  2:10 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-08 18:48 [RFC][PATCHES] getting rid of int *open in ->atomic_open() and friends Al Viro
2018-06-08 18:50 ` [PATCH 01/13] drm_mode_create_lease_ioctl(): switch to filp_clone_open() Al Viro
2018-06-08 18:50   ` [PATCH 02/13] rename filp_clone_open() to file_clone_open() Al Viro
2018-06-08 18:50   ` [PATCH 03/13] introduce FMODE_OPENED Al Viro
2018-06-08 18:50   ` [PATCH 04/13] get rid of 'opened' argument of finish_open() Al Viro
2018-06-08 18:50   ` [PATCH 05/13] pull fput() on late failures into path_openat() Al Viro
2018-06-08 18:50   ` [PATCH 06/13] switch all remaining checks for FILE_OPENED to FMODE_OPENED Al Viro
2018-06-08 18:50   ` [PATCH 07/13] now we can fold open_check_o_direct() into do_dentry_open() Al Viro
2018-06-08 18:50   ` [PATCH 08/13] __gfs2_lookup(), nfs_finish_open() and fuse_create_open() don't need 'opened' Al Viro
2018-06-08 18:50   ` [PATCH 09/13] introduce FMODE_CREATED and switch to it Al Viro
2018-06-08 18:50   ` [PATCH 10/13] IMA: don't propagate opened through the entire thing Al Viro
2018-06-08 18:50   ` [PATCH 11/13] gfs2_create_inode() doesn't need 'opened' anymore Al Viro
2018-06-08 18:50   ` [PATCH 12/13] get rid of 'opened' argument of ->atomic_open() Al Viro
2018-06-08 18:50   ` [PATCH 13/13] get rid of 'opened' in path_openat() and the helpers downstream Al Viro
2018-06-08 18:57 ` [RFC][PATCHES] getting rid of int *open in ->atomic_open() and friends Linus Torvalds
2018-06-09  5:10   ` Al Viro
2018-06-09 15:51     ` Al Viro
2018-06-11  2:10       ` Al Viro [this message]
2018-06-11 16:23         ` perfmon trouble Christoph Hellwig
2018-06-11 16:48           ` Matthew Wilcox
2018-06-11 17:04             ` Linus Torvalds
2018-06-11 17:18               ` Matthew Wilcox
2018-06-11 18:51                 ` Stephane Eranian
2018-06-11 18:59                   ` Linus Torvalds
2018-06-11 20:06                   ` Al Viro
2018-06-17 18:22     ` [RFC][PATCHES] getting rid of int *open in ->atomic_open() and friends Al Viro

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=20180611021028.GT30522@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=eranian@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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).