From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57869) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c0pK7-0003YI-H3 for qemu-devel@nongnu.org; Sun, 30 Oct 2016 08:34:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c0pK4-0001pq-Cs for qemu-devel@nongnu.org; Sun, 30 Oct 2016 08:34:51 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:58627 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c0pK4-0001pa-7k for qemu-devel@nongnu.org; Sun, 30 Oct 2016 08:34:48 -0400 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id u9UCXOi6088010 for ; Sun, 30 Oct 2016 08:34:47 -0400 Received: from e06smtp12.uk.ibm.com (e06smtp12.uk.ibm.com [195.75.94.108]) by mx0b-001b2d01.pphosted.com with ESMTP id 26cnwuqu23-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Sun, 30 Oct 2016 08:34:47 -0400 Received: from localhost by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sun, 30 Oct 2016 12:34:46 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id B91DA17D8024 for ; Sun, 30 Oct 2016 12:36:59 +0000 (GMT) Received: from d06av08.portsmouth.uk.ibm.com (d06av08.portsmouth.uk.ibm.com [9.149.37.249]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u9UCYh3x40435812 for ; Sun, 30 Oct 2016 12:34:43 GMT Received: from d06av08.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av08.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u9UCYgDP002212 for ; Sun, 30 Oct 2016 06:34:42 -0600 From: Greg Kurz Date: Sun, 30 Oct 2016 13:34:24 +0100 In-Reply-To: <1477830868-12274-1-git-send-email-groug@kaod.org> References: <1477830868-12274-1-git-send-email-groug@kaod.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Message-Id: <1477830868-12274-4-git-send-email-groug@kaod.org> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 3/7] 9pfs: fix integer overflow issue in xattr read/write List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , "Aneesh Kumar K.V" , Greg Kurz , Li Qiang From: Li Qiang The v9fs_xattr_read() and v9fs_xattr_write() are passed a guest originated offset: they must ensure this offset does not go beyond the size of the extended attribute that was set in v9fs_xattrcreate(). Unfortunately, the current code implement these checks with unsafe calculations on 32 and 64 bit values, which may allow a malicious guest to cause OOB access anyway. Fix this by comparing the offset and the xattr size, which are both uint64_t, before trying to compute the effective number of bytes to read or write. Suggested-by: Greg Kurz Signed-off-by: Li Qiang Reviewed-by: Greg Kurz Reviewed-By: Guido G=C3=BCnther --- hw/9pfs/9p.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index ab18ef2adf32..7705ead4b2ac 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -1637,20 +1637,17 @@ static int v9fs_xattr_read(V9fsState *s, V9fsPDU = *pdu, V9fsFidState *fidp, { ssize_t err; size_t offset =3D 7; - int read_count; - int64_t xattr_len; + uint64_t read_count; V9fsVirtioState *v =3D container_of(s, V9fsVirtioState, state); VirtQueueElement *elem =3D v->elems[pdu->idx]; =20 - xattr_len =3D fidp->fs.xattr.len; - read_count =3D xattr_len - off; + if (fidp->fs.xattr.len < off) { + read_count =3D 0; + } else { + read_count =3D fidp->fs.xattr.len - off; + } if (read_count > max_count) { read_count =3D max_count; - } else if (read_count < 0) { - /* - * read beyond XATTR value - */ - read_count =3D 0; } err =3D pdu_marshal(pdu, offset, "d", read_count); if (err < 0) { @@ -1979,23 +1976,18 @@ static int v9fs_xattr_write(V9fsState *s, V9fsPDU= *pdu, V9fsFidState *fidp, { int i, to_copy; ssize_t err =3D 0; - int write_count; - int64_t xattr_len; + uint64_t write_count; size_t offset =3D 7; =20 =20 - xattr_len =3D fidp->fs.xattr.len; - write_count =3D xattr_len - off; - if (write_count > count) { - write_count =3D count; - } else if (write_count < 0) { - /* - * write beyond XATTR value len specified in - * xattrcreate - */ + if (fidp->fs.xattr.len < off) { err =3D -ENOSPC; goto out; } + write_count =3D fidp->fs.xattr.len - off; + if (write_count > count) { + write_count =3D count; + } err =3D pdu_marshal(pdu, offset, "d", write_count); if (err < 0) { return err; --=20 2.5.5