1 Star 0 Fork 20

geruijun/python-rtslib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0002-saveconfig-open-the-temp-configfile-with-modes-set.patch 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
wj196 提交于 2020-07-17 15:10 . fix CVE-2020-14019
From dffcf83bead64e959505d64ad587768647caab3a Mon Sep 17 00:00:00 2001
From: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
Date: Thu, 28 May 2020 19:53:04 +0530
Subject: [PATCH] saveconfig: open the temp configfile with modes set
Fixes: #161
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
---
rtslib/root.py | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/rtslib/root.py b/rtslib/root.py
index afe1a53..a101edd 100644
--- a/rtslib/root.py
+++ b/rtslib/root.py
@@ -461,8 +461,25 @@ class RTSRoot(CFSNode):
tmp_file = save_file + ".temp"
- with open(tmp_file, "w+") as f:
- os.fchmod(f.fileno(), stat.S_IRUSR | stat.S_IWUSR)
+ mode = stat.S_IRUSR | stat.S_IWUSR # 0o600
+ umask = 0o777 ^ mode # Prevents always downgrading umask to 0
+
+ # For security, remove file with potentially elevated mode
+ try:
+ os.remove(tmp_file)
+ except OSError:
+ pass
+
+ umask_original = os.umask(umask)
+ # Even though the old file is first deleted, a race condition is still
+ # possible. Including os.O_EXCL with os.O_CREAT in the flags will
+ # prevent the file from being created if it exists due to a race
+ try:
+ fdesc = os.open(tmp_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL, mode)
+ finally:
+ os.umask(umask_original)
+
+ with os.fdopen(fdesc, 'w+') as f:
f.write(json.dumps(saveconf, sort_keys=True, indent=2))
f.write("\n")
f.flush()
--
1.8.3.1
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/geruijun/python-rtslib.git
git@gitee.com:geruijun/python-rtslib.git
geruijun
python-rtslib
python-rtslib
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385