Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751778AbbFKFij (ORCPT ); Thu, 11 Jun 2015 01:38:39 -0400 Received: from linuxhacker.ru ([217.76.32.60]:58849 "EHLO fiona.linuxhacker.ru" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1750737AbbFKFiS (ORCPT ); Thu, 11 Jun 2015 01:38:18 -0400 From: green@linuxhacker.ru To: Greg Kroah-Hartman , devel@driverdev.osuosl.org, Andreas Dilger Cc: Linux Kernel Mailing List , Oleg Drokin Subject: [PATCH v2 2/2] staging/lustre/llite: fix ll_getname user buffer copy Date: Thu, 11 Jun 2015 01:37:51 -0400 Message-Id: <1434001071-30512-3-git-send-email-green@linuxhacker.ru> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1434001071-30512-1-git-send-email-green@linuxhacker.ru> References: <1434001071-30512-1-git-send-email-green@linuxhacker.ru> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2157 Lines: 71 From: Oleg Drokin strncpy_from_user could return negative values on error, so need to take those into account. Since ll_getname is used to get a single component name from userspace to transfer to server as-is, there's no need to allocate 4k buffer as done by __getname. Allocate NAME_MAX+1 buffer instead to ensure we have enough for a null terminated max valid length buffer. This was discovered by Al Viro in https://lkml.org/lkml/2015/4/11/243 Signed-off-by: Oleg Drokin --- drivers/staging/lustre/lustre/llite/dir.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/staging/lustre/lustre/llite/dir.c b/drivers/staging/lustre/lustre/llite/dir.c index 87a042c..50d685b 100644 --- a/drivers/staging/lustre/lustre/llite/dir.c +++ b/drivers/staging/lustre/lustre/llite/dir.c @@ -1213,29 +1213,34 @@ out: return rc; } -static char * -ll_getname(const char __user *filename) +/* This function tries to get a single name component, + * to send to the server. No actual path traversal involved, + * so we limit to NAME_MAX */ +static char *ll_getname(const char __user *filename) { int ret = 0, len; - char *tmp = __getname(); + char *tmp; + tmp = kzalloc(NAME_MAX + 1, GFP_KERNEL); if (!tmp) return ERR_PTR(-ENOMEM); - len = strncpy_from_user(tmp, filename, PATH_MAX); - if (len == 0) + len = strncpy_from_user(tmp, filename, NAME_MAX + 1); + if (len < 0) + ret = len; + else if (len == 0) ret = -ENOENT; - else if (len > PATH_MAX) + else if (len > NAME_MAX && tmp[NAME_MAX] != 0) ret = -ENAMETOOLONG; if (ret) { - __putname(tmp); + kfree(tmp); tmp = ERR_PTR(ret); } return tmp; } -#define ll_putname(filename) __putname(filename) +#define ll_putname(filename) kfree(filename) static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { -- 2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/