Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3318763imu; Sun, 11 Nov 2018 12:15:50 -0800 (PST) X-Google-Smtp-Source: AJdET5fKLqmR6gfTGe/mq2tGlY0W0TOBzjagLr0mWcZ0Ta0ml36YYnogMFzBRpkXWKiK0sTwC68y X-Received: by 2002:a63:1e17:: with SMTP id e23mr14830260pge.130.1541967350058; Sun, 11 Nov 2018 12:15:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541967350; cv=none; d=google.com; s=arc-20160816; b=D5ygAp1ZjEEa6WaDs2LJ+btXakYwFfNCHk0nHUN0GTyALDPnWarJYpHt+NECGlkpK5 BObplRjY2xEfxuQQnBeIgsPCH7n5wwJ7atRHMtHr3RCf3DpAJzEBuFy85KcVqy2N9XFT f9ui6P1rjm6J6qdtHPtX80Y2Bbi7Etr5Dm/X1zG+meYk7dEpDpZO17PmNNXnGjld5kRw KWgMuH+ckW2hn1ki7B4EdkUGPqTARHpXluQCF/lGwgcmx+b35JoPTwx9X3hZ7259bH9D a+idA6xEPc5xpXqFwakQVvsWOKkYM2S/7zk1QD9EdSmTZaJYq3c8DqfMvDr0OaewvxSU VlSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=3pdjBzbqzu4DMfJ0+OGeNGDCK16IyDRa5mosQvKhH1I=; b=AW9oZ5zBz/lEbJuxJNZl9PkByajuVYQpLL+yK+N3vA7JZN6uBd/mI6cQLe6RXh6pSk zzFlMDGp3ddT7dNQEAUM0m+p9yFaI5aBzWck3ihRLTY9GZ6+XTcE+78xqDBr/IBuWBik pTJY4ECrMG04cEdnV+Z4zaHA0BGidxfRuEYyDvF9J4I8OvdMsqNvMJh9OBcXlAML0qLl boFwGrsuagee7kx69wE3JgAFeGSd37105zlg9dkgGpHYrXAfS6BV3qeV5M93dPIfiBKB D6rWLYc5O0/yP6DNnkmgOLhe5KivPSjV2NKuzgVoRrwEYU3CQh5fgtxTCpvjagOnaNAM +5zg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g7-v6si15977159plb.426.2018.11.11.12.15.34; Sun, 11 Nov 2018 12:15:50 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731530AbeKLGEn (ORCPT + 99 others); Mon, 12 Nov 2018 01:04:43 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:52968 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729602AbeKLGEn (ORCPT ); Mon, 12 Nov 2018 01:04:43 -0500 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLvt6-0000l8-0z; Sun, 11 Nov 2018 19:59:16 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gLvsQ-0001V3-QL; Sun, 11 Nov 2018 19:58:34 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Trond Myklebust" , "Stephen Johnston" , "Dave Wysochanski" Date: Sun, 11 Nov 2018 19:49:05 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 103/366] NFSv4: Fix possible 1-byte stack overflow in nfs_idmap_read_and_verify_message In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.61-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Dave Wysochanski commit d68894800ec5712d7ddf042356f11e36f87d7f78 upstream. In nfs_idmap_read_and_verify_message there is an incorrect sprintf '%d' that converts the __u32 'im_id' from struct idmap_msg to 'id_str', which is a stack char array variable of length NFS_UINT_MAXLEN == 11. If a uid or gid value is > 2147483647 = 0x7fffffff, the conversion overflows into a negative value, for example: crash> p (unsigned) (0x80000000) $1 = 2147483648 crash> p (signed) (0x80000000) $2 = -2147483648 The '-' sign is written to the buffer and this causes a 1 byte overflow when the NULL byte is written, which corrupts kernel stack memory. If CONFIG_CC_STACKPROTECTOR_STRONG is set we see a stack-protector panic: [11558053.616565] Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: ffffffffa05b8a8c [11558053.639063] CPU: 6 PID: 9423 Comm: rpc.idmapd Tainted: G W ------------ T 3.10.0-514.el7.x86_64 #1 [11558053.641990] Hardware name: Red Hat OpenStack Compute, BIOS 1.10.2-3.el7_4.1 04/01/2014 [11558053.644462] ffffffff818c7bc0 00000000b1f3aec1 ffff880de0f9bd48 ffffffff81685eac [11558053.646430] ffff880de0f9bdc8 ffffffff8167f2b3 ffffffff00000010 ffff880de0f9bdd8 [11558053.648313] ffff880de0f9bd78 00000000b1f3aec1 ffffffff811dcb03 ffffffffa05b8a8c [11558053.650107] Call Trace: [11558053.651347] [] dump_stack+0x19/0x1b [11558053.653013] [] panic+0xe3/0x1f2 [11558053.666240] [] ? kfree+0x103/0x140 [11558053.682589] [] ? idmap_pipe_downcall+0x1cc/0x1e0 [nfsv4] [11558053.689710] [] __stack_chk_fail+0x1b/0x30 [11558053.691619] [] idmap_pipe_downcall+0x1cc/0x1e0 [nfsv4] [11558053.693867] [] rpc_pipe_write+0x56/0x70 [sunrpc] [11558053.695763] [] vfs_write+0xbd/0x1e0 [11558053.702236] [] ? task_work_run+0xac/0xe0 [11558053.704215] [] SyS_write+0x7f/0xe0 [11558053.709674] [] system_call_fastpath+0x16/0x1b Fix this by calling the internally defined nfs_map_numeric_to_string() function which properly uses '%u' to convert this __u32. For consistency, also replace the one other place where snprintf is called. Signed-off-by: Dave Wysochanski Reported-by: Stephen Johnston Fixes: cf4ab538f1516 ("NFSv4: Fix the string length returned by the idmapper") Signed-off-by: Trond Myklebust [bwh: Backported to 3.16: adjust filename] Signed-off-by: Ben Hutchings --- fs/nfs/idmap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/fs/nfs/idmap.c +++ b/fs/nfs/idmap.c @@ -339,7 +339,7 @@ static ssize_t nfs_idmap_lookup_name(__u int id_len; ssize_t ret; - id_len = snprintf(id_str, sizeof(id_str), "%u", id); + id_len = nfs_map_numeric_to_string(id, id_str, sizeof(id_str)); ret = nfs_idmap_get_key(id_str, id_len, type, buf, buflen, idmap); if (ret < 0) return -EINVAL; @@ -636,7 +636,8 @@ static int nfs_idmap_read_and_verify_mes if (strcmp(upcall->im_name, im->im_name) != 0) break; /* Note: here we store the NUL terminator too */ - len = sprintf(id_str, "%d", im->im_id) + 1; + len = 1 + nfs_map_numeric_to_string(im->im_id, id_str, + sizeof(id_str)); ret = nfs_idmap_instantiate(key, authkey, id_str, len); break; case IDMAP_CONV_IDTONAME: