Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp1539621pxx; Fri, 30 Oct 2020 12:24:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9+4tOatosGtvQ9GNT+/iXp6vW7QQreWHtW6Cgl73y4+ffwbmOexQKZ/fey3H+1I8Pmg0f X-Received: by 2002:aa7:d582:: with SMTP id r2mr3984637edq.170.1604085842526; Fri, 30 Oct 2020 12:24:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1604085842; cv=none; d=google.com; s=arc-20160816; b=wPkMzYAoR9gtT4jgPQTJmQAfpHkA7Awq3P3m99eYPM/K4TID68mwxPmn+kGFTtxGNb ZZXbHO5bsbLUmZGw2OFrGcdu7vf0fQRsb/FuzIuv3+qAFKq9c3gI/hOGpgSEEbSxc3YF 5KAD85JvcSBdfa8KDYZJ+S6aqAUhNYABqFCBmX+OLpZqAAFfBBg38lh4PhXJXOxtW984 baVDiNYzAmKIrL7o9fNI3DZZchL9c2D4pl3aAlsrrhOmTkVbNDaeDKGIjFYK0hbzSvRJ EIZalUpaI+8xrVtkqF4gVjw8xfnN84q6Uv8bBzCvD/bU78R1DPpnZBkNTLdn9qB5Ubmr AMKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=wG/u6QLDIeO3+GPNzepwWscTWygaEgqcNOvOguEOtzE=; b=Y5HIoAQ1c8A4c4IRQrQG8+Ik45qvjplufpKh7wm4/7/EBA9oP4xEwM3SWzwc99M7vI r/snYrL7zXOpQQ8teAPi9mMkfYGrSO7ffm75slnJUpknkcC9DUg1+1N6K4SunHwoefxn Gpaw862+a2huUqeWYm4Ir08FOoKD9B7rGaSfgACiVZSwkFyrmVHYsd2syiZQwpHWDaDK kvLCSsW2HRTH9Adc2siTLqIgRlMYwq0+lkyHgVsbzoSNFrIXNK/EBELHCS493BCu3qYo wzR8HbTnuJu9ODDNNQTU93h0qXBx1QlUg9e+vDBY6fP7xPHQpo1HqL6oos7JOlj7cOaH y/og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="GEScQJl/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r15si1958699ejb.482.2020.10.30.12.23.38; Fri, 30 Oct 2020 12:24:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="GEScQJl/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727430AbgJ3TPG (ORCPT + 99 others); Fri, 30 Oct 2020 15:15:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727416AbgJ3TPG (ORCPT ); Fri, 30 Oct 2020 15:15:06 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABF94C0613D2 for ; Fri, 30 Oct 2020 12:15:05 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id i6so9286174lfd.1 for ; Fri, 30 Oct 2020 12:15:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wG/u6QLDIeO3+GPNzepwWscTWygaEgqcNOvOguEOtzE=; b=GEScQJl/XRE+Al+2evygNVW34IiBSkoia8s5HzAl8Hv6DEbsHP21Lirga/6tIsjkoR 6pndjXo3bhKZOW1Dk5Uub2F/QyY+XpBeX+nLyHZxDUaPhBwraxjKmyUU389u1/xqXlL/ voC7OY6hmd5Fr4gE/vpOFEq+3M/K4uBWKjshMkpddljKtLvb7oEgjUepvGxdHLB+qlRi lsXaJFer4KS7uPl33OEqYL9fA3HJYLgu76FbsgqoBkyILSU1vMZhuNLi6w9qOqfkXaAU ytPJv1sI1qSIE1x7QKh8O++0G+mtEs+XlsK0dEAC2cz1JpC6LyJR8sRuqTs2bq0q6mTh QDIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wG/u6QLDIeO3+GPNzepwWscTWygaEgqcNOvOguEOtzE=; b=mWCIgZiE3a77YHKuB27WeMxFAy2C2zCoahAZjicJHwv746e5dZJblaz8HgR9b2A0ij 9LS36Iv8PGqXEau09vw51vy6powDFZLZntzvLXn4+LOwioBYg4ZGV0n8F7QxRvkrUKn1 VCZME/U2dzEqzgif7Q5EuFZXU79r8TbNSTgCYoDQ+8PpF6Q7EsPdSYgP6omabfx3LkTl t6jdgbph4l76WCb9TNR5l1c4W4DjZRtF7D/tzoqlPFNMjuUZ2wwxOD+8apmZDsgpF6tY xZ4VRz5fHFe9qvmNfASa5hz+4brCpeWYp727jrRwkKReugBTJRR4ybypsyjcTTj2QUeA 0slA== X-Gm-Message-State: AOAM533kpADyvD6Ayh7qdy8EMOGrzZbDnzQs1ErlgezOkPk/hTjoAZ9v UtwGC63K344or7G7/QNK3rRdlhzQQN2ismymTjaiyQ== X-Received: by 2002:a05:6512:1054:: with SMTP id c20mr1626841lfb.576.1604085303713; Fri, 30 Oct 2020 12:15:03 -0700 (PDT) MIME-Version: 1.0 References: <63598b4f-6ce3-5a11-4552-cdfe308f68e4@gmail.com> <0de41eb1-e1fd-85da-61b7-fac4e3006726@gmail.com> In-Reply-To: <0de41eb1-e1fd-85da-61b7-fac4e3006726@gmail.com> From: Jann Horn Date: Fri, 30 Oct 2020 20:14:37 +0100 Message-ID: Subject: Re: For review: seccomp_user_notif(2) manual page [v2] To: "Michael Kerrisk (man-pages)" Cc: Kees Cook , Tycho Andersen , Sargun Dhillon , Christian Brauner , Daniel Borkmann , Giuseppe Scrivano , Song Liu , Robert Sesek , Containers , linux-man , lkml , Aleksa Sarai , Alexei Starovoitov , Will Drewry , bpf , Andy Lutomirski Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 29, 2020 at 3:19 PM Michael Kerrisk (man-pages) wrote: > On 10/29/20 2:42 AM, Jann Horn wrote: > > On Mon, Oct 26, 2020 at 10:55 AM Michael Kerrisk (man-pages) > > wrote: > >> static bool > >> getTargetPathname(struct seccomp_notif *req, int notifyFd, > >> char *path, size_t len) > >> { > >> char procMemPath[PATH_MAX]; > >> > >> snprintf(procMemPath, sizeof(procMemPath), "/proc/%d/mem", req->pid); > >> > >> int procMemFd = open(procMemPath, O_RDONLY); > >> if (procMemFd == -1) > >> errExit("\tS: open"); > >> > >> /* Check that the process whose info we are accessing is still alive. > >> If the SECCOMP_IOCTL_NOTIF_ID_VALID operation (performed > >> in checkNotificationIdIsValid()) succeeds, we know that the > >> /proc/PID/mem file descriptor that we opened corresponds to the > >> process for which we received a notification. If that process > >> subsequently terminates, then read() on that file descriptor > >> will return 0 (EOF). */ > >> > >> checkNotificationIdIsValid(notifyFd, req->id); > >> > >> /* Read bytes at the location containing the pathname argument > >> (i.e., the first argument) of the mkdir(2) call */ > >> > >> ssize_t nread = pread(procMemFd, path, len, req->data.args[0]); > >> if (nread == -1) > >> errExit("pread"); > > > > As discussed at > > , > > we need to re-check checkNotificationIdIsValid() after reading remote > > memory but before using the read value in any way. Otherwise, the > > syscall could in the meantime get interrupted by a signal handler, the > > signal handler could return, and then the function that performed the > > syscall could free() allocations or return (thereby freeing buffers on > > the stack). > > > > In essence, this pread() is (unavoidably) a potential use-after-free > > read; and to make that not have any security impact, we need to check > > whether UAF read occurred before using the read value. This should > > probably be called out elsewhere in the manpage, too... > > Thanks very much for pointing me at this! > > So, I want to conform that the fix to the code is as simple as > adding a check following the pread() call, something like: > > [[ > ssize_t nread = pread(procMemFd, path, len, req->data.args[argNum]); > if (nread == -1) > errExit("Supervisor: pread"); > > if (nread == 0) { > fprintf(stderr, "\tS: pread() of /proc/PID/mem " > "returned 0 (EOF)\n"); > exit(EXIT_FAILURE); > } > > if (close(procMemFd) == -1) > errExit("Supervisor: close-/proc/PID/mem"); > > + /* Once again check that the notification ID is still valid. The > + case we are particularly concerned about here is that just > + before we fetched the pathname, the target's blocked system > + call was interrupted by a signal handler, and after the handler > + returned, the target carried on execution (past the interrupted > + system call). In that case, we have no guarantees about what we > + are reading, since the target's memory may have been arbitrarily > + changed by subsequent operations. */ > + > + if (!notificationIdIsValid(notifyFd, req->id, "post-open")) > + return false; > + > /* We have no guarantees about what was in the memory of the target > process. We therefore treat the buffer returned by pread() as > untrusted input. The buffer should be terminated by a null byte; > if not, then we will trigger an error for the target process. */ > > if (strnlen(path, nread) < nread) > return true; > ]] Yeah, that should do the job. With the caveat that a cancelled syscall could've also led to the memory being munmap()ed, so the nread==0 case could also happen legitimately - so you might want to move this check up above the nread==0 (mm went away) and nread==-1 (mm still exists, but read from address failed, errno EIO) checks if the error message shouldn't appear spuriously.