From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759265AbbFBPK7 (ORCPT ); Tue, 2 Jun 2015 11:10:59 -0400 Received: from cantor2.suse.de ([195.135.220.15]:42586 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751691AbbFBPKo (ORCPT ); Tue, 2 Jun 2015 11:10:44 -0400 From: Jan Kara To: Linus Torvalds Cc: Andrew Morton , LKML , Jan Kara Subject: [PATCH 2/2] lib: Limit strnlen_user() return value to count + 1 Date: Tue, 2 Jun 2015 17:10:29 +0200 Message-Id: <1433257829-1743-2-git-send-email-jack@suse.cz> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1433257829-1743-1-git-send-email-jack@suse.cz> References: <1433257829-1743-1-git-send-email-jack@suse.cz> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently strnlen_user() can return numbers between 0 and count + sizeof(unsigned long) - 1. Currently, no in tree users seem to care but I have found out of tree users which were broken by this. They wanted to truncate the string if it was too long to fit into a buffer and didn't count with the fact that strnlen_user() can return more. So make the function harder to use wrong and return count + 1 max. CC: Linus Torvalds Signed-off-by: Jan Kara --- lib/strnlen_user.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/strnlen_user.c b/lib/strnlen_user.c index fd03ae980013..2e47f9e16a79 100644 --- a/lib/strnlen_user.c +++ b/lib/strnlen_user.c @@ -54,7 +54,10 @@ static inline long do_strnlen_user(const char __user *src, unsigned long count, if (has_zero(c, &data, &constants)) { data = prep_zero_mask(c, data, &constants); data = create_zero_mask(data); - return res + find_zero(data) + 1 - align; + res = res + find_zero(data) + 1 - align; + if (res > count) + return count + 1; + return res; } res += sizeof(unsigned long); if (unlikely(max <= sizeof(unsigned long))) -- 2.1.4