Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1793550rwb; Tue, 29 Nov 2022 20:01:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf4Mgwu4fyjQFyK/JpI+neWq5b7sNXW8KcAGjIcPcS3WATk7M2I7odlNARO3WZLtZSta3pU2 X-Received: by 2002:a05:6402:4:b0:463:cb99:5c8 with SMTP id d4-20020a056402000400b00463cb9905c8mr53738391edu.395.1669780916630; Tue, 29 Nov 2022 20:01:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669780916; cv=none; d=google.com; s=arc-20160816; b=0ogmJJ5wGA9DCdSx/a8FEU/2E5BUZ2jHSdFw8zzZjX2VnoC8ipP64MX1hLe5dRrwla /iXEhUHEvd3x7XIV3Ft+2h2ImzQMO3voEb5AevaRogKBuxuRjavWRRKjX0nqn4Z3n/Yi mep5JcaLvOYhms9ZVyofl5z8ucEQTGk3UJ5lYm8FSPyFc+u6+eMCel/ZuHTKeTj3rQCt K6PkXAtWmzfYPZ1V0Ra9C4zZzZiiSYB1r3Db8HfYUkyQ6q1BrqnIJ+vQw/WUaRt4G3iF xsyGxceK1lVGp9dTO4RqBdbezobUyoCQa5uxTJr21qB2Jrc1Om3c2rmRdCcb58hO8euT SH8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=IbgjxXco2ICYz4eUnp8sVnVcMsNs7Pg8hjijnkTmMjU=; b=AvkqS8xIUoU5qObfwiQvOm9N0/4cUX71tabqNeyAt06wh288G/Srnen5kTuu36RntK 7G40wxKIX0LRk8wtrjPBnadtxtwMK2Pz9V9mLuzs90y6yCeNM36QmTrbOZnjwYjNAZcN tLUWsgd6AslIZwvjZL9aP6rlaeQTuWp1Tcby956qsKnBl7+12fS2RU1GF+UbjLC8XwUH 1/rVcgdLNKqyNTMbcO+hhem+XgK1r1ObDYOpmt0vLCJf8y0A2zGS131gxQgla3WHMM0I e2UA+R550MoujeKd48y1AxfkUdZL0jvzk88ZPQ8T1YtA/JXa71BAanbpTgByIfBPuS9r EJfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UHy4vNcQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm8-20020a170907948800b007c08578ed86si366194ejc.960.2022.11.29.20.01.35; Tue, 29 Nov 2022 20:01:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UHy4vNcQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233095AbiK3D6n (ORCPT + 86 others); Tue, 29 Nov 2022 22:58:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232618AbiK3D6Z (ORCPT ); Tue, 29 Nov 2022 22:58:25 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12C5069DD6 for ; Tue, 29 Nov 2022 19:58:24 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d2-20020a170902cec200b001899479b1d8so6435676plg.22 for ; Tue, 29 Nov 2022 19:58:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IbgjxXco2ICYz4eUnp8sVnVcMsNs7Pg8hjijnkTmMjU=; b=UHy4vNcQIx9t/WwFNmy5jfp9/nM/BZmdBFjznHBzmbCuI5CcfSueXovUoveXMtM7DO NJ7ycoBF1WcHSkotHoiF0M+1tKeXPLXYfGFmnOamSkfT/TNHOQuwjZfqkhKUArePyvKu g6JWXT6/7LlHK3Z9bsIvcMs9xS23QZCmxsC/LzEmVoApbfgIT2su6wySuvwQKRmoNtIh HgkNnXnqM9nL79zkPMENXD75mI85RZ+LqRxcwOEqBY6jmIr5/OxOibyR8fqdm+1AjonR gGIanVj4M65jME3iVwyk84QuXDGDpnNo4ZeM1e8btTLHftEQEJLxlEGRExrlL73AFyif 1MmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IbgjxXco2ICYz4eUnp8sVnVcMsNs7Pg8hjijnkTmMjU=; b=lf/sQdaiUXpZHt1W5jmSzolLCBWnbYhn/g7dNWqLfbNGSCBbfuwLtok36dB1mtJM6F hzxCbhrHarW5xKhn+xmHlK85EwfH+jKadOCWMTDbwSxaLqtEudMZxE9xtaeb9AIcS7YX ca57d1trjwHXCYQr+gv7zOcnGnOkp7dlrNODJc2U+LR4A7y0U5gYaArUqZ6WDiPOGcZ5 rJcAWllHgA6RrcHi0DNdEa5z+DRrePOe/mQJj0Kjfu1W8LoDv06+1JS646yPQ3mpbCht ee+AfWF5mYeJP/ojgMzjb5qWF/Vp7kwF7Yw9GaKJpo5mEZ2A04egRPLGUmEnKcwlwtIC wR8g== X-Gm-Message-State: ANoB5pmD/go3WvlI/xliTHYC/VaA86PtXXGMyGrZGehQ/eFy2dgrEXhy See/ZLD+oLIsaDaE3y5i8gX6qXM16nJosA== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:aa7:9045:0:b0:56c:a321:eecb with SMTP id n5-20020aa79045000000b0056ca321eecbmr61328481pfo.19.1669780703628; Tue, 29 Nov 2022 19:58:23 -0800 (PST) Date: Wed, 30 Nov 2022 03:58:01 +0000 In-Reply-To: <20221130035805.1823970-1-cmllamas@google.com> Mime-Version: 1.0 References: <20221130035805.1823970-1-cmllamas@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221130035805.1823970-3-cmllamas@google.com> Subject: [PATCH 5.10 2/6] binder: read pre-translated fds from sender buffer From: Carlos Llamas To: stable@kernel.org, Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Hridya Valsaraju , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Todd Kjos , Christian Brauner , Carlos Llamas Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Todd Kjos commit 656e01f3ab54afe71bed066996fc2640881e1220 upstream. This patch is to prepare for an up coming patch where we read pre-translated fds from the sender buffer and translate them before copying them to the target. It does not change run time. The patch adds two new parameters to binder_translate_fd_array() to hold the sender buffer and sender buffer parent. These parameters let us call copy_from_user() directly from the sender instead of using binder_alloc_copy_from_buffer() to copy from the target. Also the patch adds some new alignment checks. Previously the alignment checks would have been done in a different place, but this lets us print more useful error messages. Reviewed-by: Martijn Coenen Acked-by: Christian Brauner Signed-off-by: Todd Kjos Link: https://lore.kernel.org/r/20211130185152.437403-4-tkjos@google.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Carlos Llamas --- drivers/android/binder.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 58ab76b7a787..4abb621300ec 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -2634,15 +2634,17 @@ static int binder_translate_fd(u32 fd, binder_size_t fd_offset, } static int binder_translate_fd_array(struct binder_fd_array_object *fda, + const void __user *sender_ubuffer, struct binder_buffer_object *parent, + struct binder_buffer_object *sender_uparent, struct binder_transaction *t, struct binder_thread *thread, struct binder_transaction *in_reply_to) { binder_size_t fdi, fd_buf_size; binder_size_t fda_offset; + const void __user *sender_ufda_base; struct binder_proc *proc = thread->proc; - struct binder_proc *target_proc = t->to_proc; fd_buf_size = sizeof(u32) * fda->num_fds; if (fda->num_fds >= SIZE_MAX / sizeof(u32)) { @@ -2666,7 +2668,10 @@ static int binder_translate_fd_array(struct binder_fd_array_object *fda, */ fda_offset = (parent->buffer - (uintptr_t)t->buffer->user_data) + fda->parent_offset; - if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32))) { + sender_ufda_base = (void __user *)sender_uparent->buffer + fda->parent_offset; + + if (!IS_ALIGNED((unsigned long)fda_offset, sizeof(u32)) || + !IS_ALIGNED((unsigned long)sender_ufda_base, sizeof(u32))) { binder_user_error("%d:%d parent offset not aligned correctly.\n", proc->pid, thread->pid); return -EINVAL; @@ -2675,10 +2680,9 @@ static int binder_translate_fd_array(struct binder_fd_array_object *fda, u32 fd; int ret; binder_size_t offset = fda_offset + fdi * sizeof(fd); + binder_size_t sender_uoffset = fdi * sizeof(fd); - ret = binder_alloc_copy_from_buffer(&target_proc->alloc, - &fd, t->buffer, - offset, sizeof(fd)); + ret = copy_from_user(&fd, sender_ufda_base + sender_uoffset, sizeof(fd)); if (!ret) ret = binder_translate_fd(fd, offset, t, thread, in_reply_to); @@ -3344,6 +3348,8 @@ static void binder_transaction(struct binder_proc *proc, case BINDER_TYPE_FDA: { struct binder_object ptr_object; binder_size_t parent_offset; + struct binder_object user_object; + size_t user_parent_size; struct binder_fd_array_object *fda = to_binder_fd_array_object(hdr); size_t num_valid = (buffer_offset - off_start_offset) / @@ -3375,8 +3381,27 @@ static void binder_transaction(struct binder_proc *proc, return_error_line = __LINE__; goto err_bad_parent; } - ret = binder_translate_fd_array(fda, parent, t, thread, - in_reply_to); + /* + * We need to read the user version of the parent + * object to get the original user offset + */ + user_parent_size = + binder_get_object(proc, user_buffer, t->buffer, + parent_offset, &user_object); + if (user_parent_size != sizeof(user_object.bbo)) { + binder_user_error("%d:%d invalid ptr object size: %zd vs %zd\n", + proc->pid, thread->pid, + user_parent_size, + sizeof(user_object.bbo)); + return_error = BR_FAILED_REPLY; + return_error_param = -EINVAL; + return_error_line = __LINE__; + goto err_bad_parent; + } + ret = binder_translate_fd_array(fda, user_buffer, + parent, + &user_object.bbo, t, + thread, in_reply_to); if (!ret) ret = binder_alloc_copy_to_buffer(&target_proc->alloc, t->buffer, -- 2.38.1.584.g0f3c55d4c2-goog