From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51209) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c1Xko-0002Je-Bf for qemu-devel@nongnu.org; Tue, 01 Nov 2016 08:01:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c1Xkk-0005sx-DI for qemu-devel@nongnu.org; Tue, 01 Nov 2016 08:01:22 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:52664 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 1c1Xkk-0005sd-8f for qemu-devel@nongnu.org; Tue, 01 Nov 2016 08:01:18 -0400 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id uA1Bx1ZL132055 for ; Tue, 1 Nov 2016 08:01:17 -0400 Received: from e06smtp08.uk.ibm.com (e06smtp08.uk.ibm.com [195.75.94.104]) by mx0b-001b2d01.pphosted.com with ESMTP id 26espsm62s-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 01 Nov 2016 08:01:17 -0400 Received: from localhost by e06smtp08.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 1 Nov 2016 12:01:12 -0000 From: Greg Kurz Date: Tue, 1 Nov 2016 13:00:52 +0100 In-Reply-To: <1478001656-5766-1-git-send-email-groug@kaod.org> References: <1478001656-5766-1-git-send-email-groug@kaod.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Message-Id: <1478001656-5766-4-git-send-email-groug@kaod.org> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL v2 for-2.8 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 Signed-off-by: Greg Kurz --- 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