From nobody Sun Feb 8 22:57:55 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1563901650; cv=none; d=zoho.com; s=zohoarc; b=KdXhtArGG/IcZ9D03E6ZP3hsENJE2fDEDgKu1nMeT7JKtkk1k9A7jt4aFCHpDtm4BB7SNm/8jbCf7iM6+iz5AeKudz6p12OLzvckAvEeqETYsb4BkkUMcGUjzWMlEpwTeHgoSEa3UebmeDqF6CrhIUoSGMi30eyiSNiIUd+xXiw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563901650; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=4L41p/KexEyP+6kNYZ686Lqo0ks9Gqq2lWeLqrt0Zuc=; b=jqYf2AYy/LIDgApH+wLj19ygtSFGItg2GIORo4dY6McPLIXffMsy3SDThROdKZJE+ANuv2X0An5RWn5xFbdPq8rk2BxbeSRF0glN8JphbI+l3sQf60Szaj7B0HRUwGTlkgO39uX6tncPm/m7bqiqjDAu4LQOz1TMI6q9i0R3aVQ= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1563901650247493.8747992764503; Tue, 23 Jul 2019 10:07:30 -0700 (PDT) Received: from localhost ([::1]:45944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpyG4-0007VL-IA for importer@patchew.org; Tue, 23 Jul 2019 13:07:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42713) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpyAX-0004wW-3W for qemu-devel@nongnu.org; Tue, 23 Jul 2019 13:01:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hpyAU-0005Mx-1P for qemu-devel@nongnu.org; Tue, 23 Jul 2019 13:01:40 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:16636) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hpyAS-0005BX-SK for qemu-devel@nongnu.org; Tue, 23 Jul 2019 13:01:37 -0400 Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x6NGkgvk127106 for ; Tue, 23 Jul 2019 13:01:25 -0400 Received: from e31.co.us.ibm.com (e31.co.us.ibm.com [32.97.110.149]) by mx0a-001b2d01.pphosted.com with ESMTP id 2tx60rgmv9-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 23 Jul 2019 13:01:25 -0400 Received: from localhost by e31.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 23 Jul 2019 18:01:25 +0100 Received: from b03cxnp07028.gho.boulder.ibm.com (9.17.130.15) by e31.co.us.ibm.com (192.168.1.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 23 Jul 2019 18:01:22 +0100 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp07028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x6NH1LvS49480174 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 23 Jul 2019 17:01:22 GMT Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E01BBBE05D; Tue, 23 Jul 2019 17:01:21 +0000 (GMT) Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C7BE1BE05B; Tue, 23 Jul 2019 17:01:21 +0000 (GMT) Received: from localhost (unknown [9.53.179.212]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 23 Jul 2019 17:01:21 +0000 (GMT) From: Michael Roth To: qemu-devel@nongnu.org Date: Tue, 23 Jul 2019 12:00:53 -0500 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190723170104.4327-1-mdroth@linux.vnet.ibm.com> References: <20190723170104.4327-1-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19072317-8235-0000-0000-00000EBD2AA5 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00011482; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000287; SDB=6.01236375; UDB=6.00651638; IPR=6.01017731; MB=3.00027856; MTD=3.00000008; XFM=3.00000015; UTC=2019-07-23 17:01:24 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19072317-8236-0000-0000-0000468424BA Message-Id: <20190723170104.4327-26-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-07-23_07:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=734 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1906280000 definitions=main-1907230169 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH 25/36] usb-mtp: use O_NOFOLLOW and O_CLOEXEC. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bandan Das , Prasad J Pandit , qemu-stable@nongnu.org, Gerd Hoffmann Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Gerd Hoffmann Open files and directories with O_NOFOLLOW to avoid symlinks attacks. While being at it also add O_CLOEXEC. usb-mtp only handles regular files and directories and ignores everything else, so users should not see a difference. Because qemu ignores symlinks, carrying out a successful symlink attack requires swapping an existing file or directory below rootdir for a symlink and winning the race against the inotify notification to qemu. Fixes: CVE-2018-16872 Cc: Prasad J Pandit Cc: Bandan Das Reported-by: Michael Hanselmann Signed-off-by: Gerd Hoffmann Reviewed-by: Michael Hanselmann Message-id: 20181213122511.13853-1-kraxel@redhat.com (cherry picked from commit bab9df35ce73d1c8e19a37e2737717ea1c984dc1) Signed-off-by: Michael Roth --- hw/usb/dev-mtp.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 100b7171f4..36c43b8c20 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -653,13 +653,18 @@ static void usb_mtp_object_readdir(MTPState *s, MTPOb= ject *o) { struct dirent *entry; DIR *dir; + int fd; =20 if (o->have_children) { return; } o->have_children =3D true; =20 - dir =3D opendir(o->path); + fd =3D open(o->path, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); + if (fd < 0) { + return; + } + dir =3D fdopendir(fd); if (!dir) { return; } @@ -1007,7 +1012,7 @@ static MTPData *usb_mtp_get_object(MTPState *s, MTPCo= ntrol *c, =20 trace_usb_mtp_op_get_object(s->dev.addr, o->handle, o->path); =20 - d->fd =3D open(o->path, O_RDONLY); + d->fd =3D open(o->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); if (d->fd =3D=3D -1) { usb_mtp_data_free(d); return NULL; @@ -1031,7 +1036,7 @@ static MTPData *usb_mtp_get_partial_object(MTPState *= s, MTPControl *c, c->argv[1], c->argv[2]); =20 d =3D usb_mtp_data_alloc(c); - d->fd =3D open(o->path, O_RDONLY); + d->fd =3D open(o->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); if (d->fd =3D=3D -1) { usb_mtp_data_free(d); return NULL; @@ -1658,7 +1663,7 @@ static void usb_mtp_write_data(MTPState *s) 0, 0, 0, 0); goto done; } - d->fd =3D open(path, O_CREAT | O_WRONLY, mask); + d->fd =3D open(path, O_CREAT | O_WRONLY | O_CLOEXEC | O_NOFOLLOW, = mask); if (d->fd =3D=3D -1) { usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, 0, 0, 0, 0); --=20 2.17.1