Skip to content

Commit

Permalink
python3: patch CVE-2015-20107. (#3911)
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelWMS authored Oct 4, 2022
1 parent 422bb65 commit 8ad42eb
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
110 changes: 110 additions & 0 deletions SPECS/python3/CVE-2015-20107.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
From c3e7f139b440d7424986204e9f3fc2275aea3377 Mon Sep 17 00:00:00 2001
From: Petr Viktorin <[email protected]>
Date: Wed, 27 Apr 2022 18:17:33 +0200
Subject: [PATCH] gh-68966: Make mailcap refuse to match unsafe
filenames/types/params

---
Lib/mailcap.py | 26 ++++++++++++++++++++++++--
Lib/test/test_mailcap.py | 8 ++++++--
2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/Lib/mailcap.py b/Lib/mailcap.py
index 856b6a55475f..cfb70edc61ec 100644
--- a/Lib/mailcap.py
+++ b/Lib/mailcap.py
@@ -2,6 +2,7 @@

import os
import warnings
+import re

__all__ = ["getcaps","findmatch"]

@@ -19,6 +20,11 @@ def lineno_sort_key(entry):
else:
return 1, 0

+_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@%+=:,./-]').search
+
+class UnsafeMailcapInput(Warning):
+ """Warning raised when refusing unsafe input"""
+

# Part 1: top-level interface.

@@ -171,15 +177,22 @@ def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
entry to use.

"""
+ if _find_unsafe(filename):
+ msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
+ warnings.warn(msg, UnsafeMailcapInput)
+ return None, None
entries = lookup(caps, MIMEtype, key)
# XXX This code should somehow check for the needsterminal flag.
for e in entries:
if 'test' in e:
test = subst(e['test'], filename, plist)
+ if test is None:
+ continue
if test and os.system(test) != 0:
continue
command = subst(e[key], MIMEtype, filename, plist)
- return command, e
+ if command is not None:
+ return command, e
return None, None

def lookup(caps, MIMEtype, key=None):
@@ -212,6 +225,10 @@ def subst(field, MIMEtype, filename, plist=[]):
elif c == 's':
res = res + filename
elif c == 't':
+ if _find_unsafe(MIMEtype):
+ msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
+ warnings.warn(msg, UnsafeMailcapInput)
+ return None
res = res + MIMEtype
elif c == '{':
start = i
@@ -219,7 +236,12 @@ def subst(field, MIMEtype, filename, plist=[]):
i = i+1
name = field[start:i]
i = i+1
- res = res + findparam(name, plist)
+ param = findparam(name, plist)
+ if _find_unsafe(param):
+ msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
+ warnings.warn(msg, UnsafeMailcapInput)
+ return None
+ res = res + param
# XXX To do:
# %n == number of parts if type is multipart/*
# %F == list of alternating type and filename for parts
diff --git a/Lib/test/test_mailcap.py b/Lib/test/test_mailcap.py
index 97a8fac6e074..2ed367dba78b 100644
--- a/Lib/test/test_mailcap.py
+++ b/Lib/test/test_mailcap.py
@@ -128,7 +128,8 @@ def test_subst(self):
(["", "audio/*", "foo.txt"], ""),
(["echo foo", "audio/*", "foo.txt"], "echo foo"),
(["echo %s", "audio/*", "foo.txt"], "echo foo.txt"),
- (["echo %t", "audio/*", "foo.txt"], "echo audio/*"),
+ (["echo %t", "audio/*", "foo.txt"], None),
+ (["echo %t", "audio/wav", "foo.txt"], "echo audio/wav"),
(["echo \\%t", "audio/*", "foo.txt"], "echo %t"),
(["echo foo", "audio/*", "foo.txt", plist], "echo foo"),
(["echo %{total}", "audio/*", "foo.txt", plist], "echo 3")
@@ -212,7 +213,10 @@ def test_findmatch(self):
('"An audio fragment"', audio_basic_entry)),
([c, "audio/*"],
{"filename": fname},
- ("/usr/local/bin/showaudio audio/*", audio_entry)),
+ (None, None)),
+ ([c, "audio/wav"],
+ {"filename": fname},
+ ("/usr/local/bin/showaudio audio/wav", audio_entry)),
([c, "message/external-body"],
{"plist": plist},
("showexternal /dev/null default john python.org /tmp foo bar", message_entry))
6 changes: 5 additions & 1 deletion SPECS/python3/python3.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Summary: A high-level scripting language
Name: python3
Version: 3.7.13
Release: 3%{?dist}
Release: 4%{?dist}
License: PSF
Vendor: Microsoft Corporation
Distribution: Mariner
Expand All @@ -15,6 +15,7 @@ Patch1: python3-support-mariner-platform.patch
Patch2: Replace-unsupported-TLS-methods.patch
Patch3: fix_broken_mariner_ssl_tests.patch
Patch4: CVE-2021-28861.patch
Patch5: CVE-2015-20107.patch
BuildRequires: bzip2-devel
BuildRequires: expat-devel >= 2.1.0
BuildRequires: libffi-devel >= 3.0.13
Expand Down Expand Up @@ -279,6 +280,9 @@ make %{?_smp_mflags} test
%{_libdir}/python3.7/test/*

%changelog
* Mon Oct 03 2022 Pawel Winogrodzki <[email protected]> - 3.7.13-4
- Add CVE-2015-20107 patch from upstream

* Thu Sep 01 2022 Minghe Ren <[email protected]> - 3.7.13-3
- Add patch to fix CVE-2021-28861

Expand Down

0 comments on commit 8ad42eb

Please sign in to comment.