Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp456920rwe; Fri, 26 Aug 2022 08:04:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR5931Hh9FHb3CCoh+ad7j7v4NNqSvFS6UG60Qzd27B5OkS1AOcoYnZD45IxMnzh/wLPAYRc X-Received: by 2002:a05:6402:2813:b0:447:aba7:2f8d with SMTP id h19-20020a056402281300b00447aba72f8dmr7115097ede.55.1661526288846; Fri, 26 Aug 2022 08:04:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661526288; cv=none; d=google.com; s=arc-20160816; b=0WypaRMbeC4ECs/ul4Le3ugBzsuS07mibVZDOsfecoXdYe6L3lJLoVjFRlasEXb1p3 Yk5yMbHjszcUF2b4ddcvzTd/KvtzP4RCH/WkzwQzJ8K2HrIAX4wRkOOADRAystAQguVU sqaQ1Ud1SKdmWk+x0aAjAWRMxLy3syUwARRC/oLgrO09kaWdR+f3YYi44H+7oRttmuyx sBLOKM7VWWCNmWwB4Y/fAXdY/Xln6Sa7WEqQqHiCJwLKL+PiaYvwvqonSGIv+SAXVAxI 5MIJ63EQJEm+UfBMOsGGTa/dLJgeurMluUxzk/NQXOgdrWj+qUIp/x/U7F3o4s+l4unz B2Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1dONJxU9ryy0Ovox1iosZF6jlxmH/dzZllv/GEFxJhs=; b=RKCMAWu4Cgl9n14VKwvSVQlaNqPXI2Y1qgiF0nS0GrxzKFjhDwvZ73LYFz6GPIQ1KT eMWJvzzaEfiBc1mCReZ7vxopJLmBwusQqqUXYRT8kkdl8cYPT971TKPAFiZ2bhGWZWTg zb2LF3LgrDOoIVz4K77VuB0U94pH7Z0wbuaysGJB4d+QCBiApLbWdPmYh01Xokp6+N/U +yToFtl+1rdK/MzkXzmMNonQRKmr1giWMzMrlJbZYHLq3ES/3IeMzQAQf793BGNWPON9 MeobOm+o+xu+JnsgEuk0BuIN+bQexc8/Wv6+ycHHY6/CPnQr8gcEMTggwpFxnjsrIibL e0ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=V0OpgPaZ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e9-20020a50d4c9000000b0043de10b9d72si1522191edj.315.2022.08.26.08.04.21; Fri, 26 Aug 2022 08:04:48 -0700 (PDT) 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=@intel.com header.s=Intel header.b=V0OpgPaZ; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344562AbiHZOqv (ORCPT + 99 others); Fri, 26 Aug 2022 10:46:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343947AbiHZOqr (ORCPT ); Fri, 26 Aug 2022 10:46:47 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CFFFD87D9; Fri, 26 Aug 2022 07:46:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661525206; x=1693061206; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eXJbBe/5ycXuCWYqrcqoy4JvkM5qhTcXgKIWnTL3yIE=; b=V0OpgPaZbSKDcvo2NXd1Kw6w5I7h3ooa0xDaqc9e2eRYr3ErN5aHCyh4 Dz+KNWeX3h+K+iG/z8DLTB5ohocvLa9mEhdgPEQpvnDrgXDt/F/xRus9W L/l7jSIMN+9xCjKCuZG1pIebNkzFEYRhUMBwHYOzXS5PFqRnBJyoh4wv1 ziTjXFWMaORCLWYBuFL00+8r4hwng7dQSppP6vIgle6gQIwx2tMGyTp4z 0+dH7W9/hKQY2CamZS6AFb0+PwVyKJLgds3wKxewJToUBn/+EdpGjau5f Tk1n3Rq+S9MkxQQBtSV4ncrctzMNjV23kBcYglW+sRUbtRO4vQAErJhir w==; X-IronPort-AV: E=McAfee;i="6500,9779,10451"; a="295792975" X-IronPort-AV: E=Sophos;i="5.93,265,1654585200"; d="scan'208";a="295792975" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 07:46:45 -0700 X-IronPort-AV: E=Sophos;i="5.93,265,1654585200"; d="scan'208";a="671488346" Received: from syeghiay-mobl.ger.corp.intel.com (HELO ijarvine-MOBL2.ger.corp.intel.com) ([10.252.41.100]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Aug 2022 07:46:43 -0700 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= To: Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Shevchenko , Lino Sanfilippo , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH 2/3] serial: add helpers to copy serial_rs485 from/to userspace Date: Fri, 26 Aug 2022 17:46:28 +0300 Message-Id: <20220826144629.11507-3-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220826144629.11507-1-ilpo.jarvinen@linux.intel.com> References: <20220826144629.11507-1-ilpo.jarvinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Due to the padding fields, the copying will need to a bit more than usual. Move padding clearing into the helper that copies back to userspace. Signed-off-by: Ilpo Järvinen --- drivers/tty/serial/serial_core.c | 41 +++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index 9c1bf36b7a93..6d57cfdeda9d 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -1375,10 +1375,6 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4 rs485->flags &= supported_flags; uart_sanitize_serial_rs485_delays(port, rs485); - - /* Return clean padding area to userspace */ - memset(rs485->padding0, 0, sizeof(rs485->padding0)); - memset(rs485->padding1, 0, sizeof(rs485->padding1)); } static void uart_set_rs485_termination(struct uart_port *port, @@ -1407,6 +1403,28 @@ int uart_rs485_config(struct uart_port *port) } EXPORT_SYMBOL_GPL(uart_rs485_config); +static int user_rs485_to_kernel_serial_rs485(struct serial_rs485 *rs485, + const struct serial_rs485 __user *rs485_user) +{ + if (copy_from_user(rs485, rs485_user, sizeof(*rs485))) + return -EFAULT; + + return 0; +} + +static int kernel_serial_rs485_to_user_rs485(struct serial_rs485 __user *rs485_user, + struct serial_rs485 *rs485) +{ + /* Return clean padding area to userspace */ + memset(rs485->padding0, 0, sizeof(rs485->padding0)); + memset(rs485->padding1, 0, sizeof(rs485->padding1)); + + if (copy_to_user(rs485_user, rs485, sizeof(*rs485))) + return -EFAULT; + + return 0; +} + static int uart_get_rs485_config(struct uart_port *port, struct serial_rs485 __user *rs485_user) { @@ -1417,10 +1435,7 @@ static int uart_get_rs485_config(struct uart_port *port, rs485 = port->rs485; spin_unlock_irqrestore(&port->lock, flags); - if (copy_to_user(rs485_user, &rs485, sizeof(rs485))) - return -EFAULT; - - return 0; + return kernel_serial_rs485_to_user_rs485(rs485_user, &rs485); } static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, @@ -1433,8 +1448,9 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, if (!port->rs485_config) return -ENOTTY; - if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user))) - return -EFAULT; + ret = user_rs485_to_kernel_serial_rs485(&rs485, rs485_user); + if (ret) + return ret; ret = uart_check_rs485_flags(port, &rs485); if (ret) @@ -1450,10 +1466,7 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, if (ret) return ret; - if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) - return -EFAULT; - - return 0; + return kernel_serial_rs485_to_user_rs485(rs485_user, &port->rs485); } static int uart_get_iso7816_config(struct uart_port *port, -- 2.30.2