Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10926070imu; Thu, 6 Dec 2018 08:50:52 -0800 (PST) X-Google-Smtp-Source: AFSGD/VfOwPnKLzgOdwOTMkrfmVIFbALgHJ4sVlos7IU/MT/u3iIfYOVio21KoqNlqe9FE40TiI5 X-Received: by 2002:a17:902:4025:: with SMTP id b34mr29215612pld.181.1544115052306; Thu, 06 Dec 2018 08:50:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544115052; cv=none; d=google.com; s=arc-20160816; b=qd/C9S24B94qM0t677EoUkht/LygYh+YeYFJnTzLqYM+ai+aYGD5yQOgUp8nHEVIsb yOKqm0WFObPw27sLgGr1aoI9gVTLXgacu/7nU8HUcYTNsGpv3vDTnZbAcwX+CScvRygj Wh8d4v7M095gOxw//Xnai5z6TT1Gum07JWKxGAkN9PVzAiMhwRNQEYet26uHhcXzVWhX LCBpuiF+pXuRUuCfkgzvPVpmWA+UR0/oyy9ZIs9whwjV/+4ah38bR6r60F1AWBuHI202 47cQpFNHWVyxjN8JOdB/zz0CnRpFUGs2s4whQSOI9nqvnwdZvl3av9AYiC8GkyhxV0/y rjCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=f6ZhIgPxb5JuEi5w65rXIUYHXmApKJnwNsBxtTH3yT4=; b=f0EU11kRNukxZl489HWp1Fncruaf6fbnULAm3WH2/jMV7qe+N918zYaPsSmRhsRJiB YUK0l1mk9l057susODdSEL1sOhDtTWamcLAQ6KSH19pJH+qWLpL8XAuhwvgK3gt8uIXA 2mWDSj9E9k0VSqkVKDRkhiJ2l+4jPz4OU5Cp75rhKsZLgCZd5hbSX3c2ion9LVnNG4je R8v7naS3xVF2IUPNUO017ng9EBLPKa92McmQD16IqhOHSC0EcLRXdvym2BnG/rcP5t3e 3cwvJ/q8oiANvNiPfvZYFuIfKnTGn6VCma725diDb3YWfMkb7l57QBhNeUF5u9+0O2yv v3MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BHdf0pTD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i9si647632plb.35.2018.12.06.08.50.34; Thu, 06 Dec 2018 08:50:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BHdf0pTD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726024AbeLFQtM (ORCPT + 99 others); Thu, 6 Dec 2018 11:49:12 -0500 Received: from mail-yb1-f193.google.com ([209.85.219.193]:36750 "EHLO mail-yb1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725939AbeLFQtL (ORCPT ); Thu, 6 Dec 2018 11:49:11 -0500 Received: by mail-yb1-f193.google.com with SMTP id w203so935615ybg.3 for ; Thu, 06 Dec 2018 08:49:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=f6ZhIgPxb5JuEi5w65rXIUYHXmApKJnwNsBxtTH3yT4=; b=BHdf0pTDh1fcUnEzAb5K2S1oG8rWk/mh7j7WM6Hni5W9pp++25RnRnYhX7ZmgqV3pH FerZn5Pq+xwK7tSjDNXjUrrH5t2BXhEmc61Y5RkWSDB33VRt6QRnimqH5p3bCsC5JLpP a1aIWFjjcvTJ/r+u25H3UuIRad4UFcdTFgyLE= 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=f6ZhIgPxb5JuEi5w65rXIUYHXmApKJnwNsBxtTH3yT4=; b=AUNreAVI3WWJLzOAr+0L7v+l1TfaD/mVNL4i/j7kJP4isUiHp0F892yLlfJLS8Wjkb UG4HHJdqzwX7F9Np0t/+54Y+wItWzfllIB7fTwR1OLzSjO8GZt21CYIpRDQdEafM4+hY 6MGtr64Dd5Lnrf7prRHz3WBe+baYlLgng8kXrr8DFxewAsq1Rxw2kBVYqWqAty0vAG1c WxpxxgYx/++0JZ/ZOOP8W92hNL/fwx09lR1zaXVTBjNt0VumswcxEBtxxASeyeWbVbQl UwVWAzogFN6vxNwRf6r5q9VKM/FwXR0JIyD1pLmVX9R5L0VKusfsfCaZEJ87PgkURWdt XWfA== X-Gm-Message-State: AA+aEWbbOTTel40ZiDUKYZ0HwoFNMLLZy3uDJwM+UN4Raqm5gBwEg+0Z 1AyvsmYVkpoJ7Sw0ntRCaYTeUefYisc= X-Received: by 2002:a81:6cf:: with SMTP id 198mr29246418ywg.138.1544114950309; Thu, 06 Dec 2018 08:49:10 -0800 (PST) Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com. [209.85.219.177]) by smtp.gmail.com with ESMTPSA id h62sm183896ywe.100.2018.12.06.08.49.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Dec 2018 08:49:08 -0800 (PST) Received: by mail-yb1-f177.google.com with SMTP id w203so935437ybg.3 for ; Thu, 06 Dec 2018 08:49:07 -0800 (PST) X-Received: by 2002:a81:98e:: with SMTP id 136mr28855840ywj.47.1544114947295; Thu, 06 Dec 2018 08:49:07 -0800 (PST) MIME-Version: 1.0 References: <20181206150156.28210-1-david.abdurachmanov@gmail.com> <20181206150156.28210-3-david.abdurachmanov@gmail.com> In-Reply-To: <20181206150156.28210-3-david.abdurachmanov@gmail.com> From: Kees Cook Date: Thu, 6 Dec 2018 08:48:55 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/2] riscv: fix syscall_{get,set}_arguments To: David Abdurachmanov Cc: Palmer Dabbelt , Albert Ou , Andy Lutomirski , Will Drewry , green.hu@gmail.com, deanbo422@gmail.com, LKML , linux-riscv@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 6, 2018 at 7:02 AM David Abdurachmanov wrote: > > Testing with libseccomp master branch revealed that testcases with > filters on syscall arguments were failing due to wrong values. Seccomp > uses syscall_get_argumentsi() to copy syscall arguments, and there is a > bug in pointer arithmetics in memcpy() call. > > Two alternative implementation were tested: the one in this patch and > another one based on while-break loop. Both delivered the same results. > > This implementation is also used in arm, arm64 and nds32 arches. Minor nit: can you make this the first patch? That way seccomp works correctly from the point of introduction. :) -Kees > > Signed-off-by: David Abdurachmanov > --- > arch/riscv/include/asm/syscall.h | 42 ++++++++++++++++++++++++-------- > 1 file changed, 32 insertions(+), 10 deletions(-) > > diff --git a/arch/riscv/include/asm/syscall.h b/arch/riscv/include/asm/syscall.h > index bba3da6ef157..26ceb434a433 100644 > --- a/arch/riscv/include/asm/syscall.h > +++ b/arch/riscv/include/asm/syscall.h > @@ -70,19 +70,32 @@ static inline void syscall_set_return_value(struct task_struct *task, > regs->a0 = (long) error ?: val; > } > > +#define SYSCALL_MAX_ARGS 6 > + > static inline void syscall_get_arguments(struct task_struct *task, > struct pt_regs *regs, > unsigned int i, unsigned int n, > unsigned long *args) > { > - BUG_ON(i + n > 6); > + if (n == 0) > + return; > + > + if (i + n > SYSCALL_MAX_ARGS) { > + unsigned long *args_bad = args + SYSCALL_MAX_ARGS - i; > + unsigned int n_bad = n + i - SYSCALL_MAX_ARGS; > + pr_warning("%s called with max args %d, handling only %d\n", > + __func__, i + n, SYSCALL_MAX_ARGS); > + memset(args_bad, 0, n_bad * sizeof(args[0])); > + } > + > if (i == 0) { > args[0] = regs->orig_a0; > args++; > i++; > n--; > } > - memcpy(args, ®s->a1 + i * sizeof(regs->a1), n * sizeof(args[0])); > + > + memcpy(args, ®s->a0 + i, n * sizeof(args[0])); > } > > static inline void syscall_set_arguments(struct task_struct *task, > @@ -90,14 +103,23 @@ static inline void syscall_set_arguments(struct task_struct *task, > unsigned int i, unsigned int n, > const unsigned long *args) > { > - BUG_ON(i + n > 6); > - if (i == 0) { > - regs->orig_a0 = args[0]; > - args++; > - i++; > - n--; > - } > - memcpy(®s->a1 + i * sizeof(regs->a1), args, n * sizeof(regs->a0)); > + if (n == 0) > + return; > + > + if (i + n > SYSCALL_MAX_ARGS) { > + pr_warning("%s called with max args %d, handling only %d\n", > + __func__, i + n, SYSCALL_MAX_ARGS); > + n = SYSCALL_MAX_ARGS - i; > + } > + > + if (i == 0) { > + regs->orig_a0 = args[0]; > + args++; > + i++; > + n--; > + } > + > + memcpy(®s->a0 + i, args, n * sizeof(args[0])); > } > > static inline int syscall_get_arch(void) > -- > 2.19.2 > -- Kees Cook