forked from openSUSE/rpmlint-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckSUIDPermissions.py
296 lines (250 loc) · 11.1 KB
/
CheckSUIDPermissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# vim:sw=4:et
#############################################################################
# File : CheckSUIDPermissions.py
# Package : rpmlint
# Author : Ludwig Nussel
# Purpose : Check for /etc/permissions violations
#############################################################################
from __future__ import print_function
from Filter import printWarning, printError, printInfo, addDetails
import AbstractCheck
import os
import re
import rpm
import sys
_permissions_d_whitelist = (
"lprng",
"lprng.paranoid",
"mail-server",
"mail-server.paranoid",
"postfix",
"postfix.paranoid",
"sendmail",
"sendmail.paranoid",
"squid",
"texlive",
"texlive.texlive",
"otrs", # bsc#1118049
)
class SUIDCheck(AbstractCheck.AbstractCheck):
def __init__(self):
AbstractCheck.AbstractCheck.__init__(self, "CheckSUIDPermissions")
self.perms = {}
for fname in ('/etc/permissions', '/etc/permissions.secure'):
if os.path.exists(fname):
self._parsefile(fname)
def _parsefile(self, fname):
lnr = 0
lastfn = None
with open(fname) as inputfile:
for line in inputfile:
lnr += 1
line = line.split('#')[0].split('\n')[0]
line = line.strip()
if not len(line):
continue
if line.startswith("+capabilities "):
line = line[len("+capabilities "):]
if lastfn:
self.perms[lastfn]['fscaps'] = line
continue
line = re.split(r'\s+', line.strip())
if len(line) == 3:
fn = line[0]
owner = line[1].replace('.', ':')
mode = line[2]
self.perms[fn] = {"owner": owner,
"mode": int(mode, 8) & 0o7777}
# for permissions that don't change and therefore
# don't need special handling
if fname == '/etc/permissions':
self.perms[fn]['static'] = True
else:
print('%s: Malformatted line %d: %s...' %
(fname, lnr, ' '.join(line)), file=sys.stderr)
def check(self, pkg):
global _permissions_d_whitelist
if pkg.isSource():
return
files = pkg.files()
permfiles = {}
# first pass, find and parse permissions.d files
for f in files:
if f in pkg.ghostFiles():
continue
if f.startswith("/etc/permissions.d/"):
bn = f[19:]
if bn not in _permissions_d_whitelist:
printError(pkg, "permissions-unauthorized-file", f)
bn = bn.split('.')[0]
if bn not in permfiles:
permfiles[bn] = 1
for f in permfiles:
f = pkg.dirName() + "/etc/permissions.d/" + f
if os.path.exists(f + ".secure"):
self._parsefile(f + ".secure")
else:
self._parsefile(f)
need_set_permissions = False
found_suseconfig = False
# second pass, find permissions violations
for f, pkgfile in files.items():
if pkgfile.filecaps:
printError(pkg, 'permissions-fscaps',
'%(fname)s has fscaps "%(caps)s"' %
{'fname': f, 'caps': pkgfile.filecaps})
mode = pkgfile.mode
owner = pkgfile.user + ':' + pkgfile.group
# S_IFSOCK 014 socket
# S_IFLNK 012 symbolic link
# S_IFREG 010 regular file
# S_IFBLK 006 block device
# S_IFDIR 004 directory
# S_IFCHR 002 character device
# S_IFIFO 001 FIFO
type = (mode >> 12) & 0o17
mode &= 0o7777
need_verifyscript = False
if f in self.perms or (type == 4 and f + "/" in self.perms):
if type == 0o12:
printWarning(pkg, "permissions-symlink", f)
continue
need_verifyscript = True
m = 0
o = "invalid"
if type == 4:
if f in self.perms:
printWarning(pkg, 'permissions-dir-without-slash', f)
else:
f += '/'
if type == 0o10 and mode & 0o111:
# pie binaries have 'shared object' here
if (pkgfile.magic.startswith('ELF ') and
('shared object' not in pkgfile.magic) and
('pie executable' not in pkgfile.magic)):
printError(pkg, 'non-position-independent-executable',
f)
m = self.perms[f]['mode']
o = self.perms[f]['owner']
if mode != m:
printError(
pkg, 'permissions-incorrect',
'%(file)s has mode 0%(mode)o but should be 0%(m)o' %
{'file': f, 'mode': mode, 'm': m})
if owner != o:
printError(
pkg, 'permissions-incorrect-owner',
'%(file)s belongs to %(owner)s but should be %(o)s' %
{'file': f, 'owner': owner, 'o': o})
elif type != 0o12:
if f + '/' in self.perms:
printWarning(
pkg, 'permissions-file-as-dir',
f + ' is a file but listed as directory')
if mode & 0o6000:
need_verifyscript = True
msg = '%(file)s is packaged with ' \
'setuid/setgid bits (0%(mode)o)' % \
{'file': f, 'mode': mode}
if type != 0o4:
printError(pkg, 'permissions-file-setuid-bit', msg)
else:
printWarning(pkg, 'permissions-directory-setuid-bit', msg)
if type == 0o10:
if ('shared object' not in pkgfile.magic and
'pie executable' not in pkgfile.magic):
printError(pkg, 'non-position-independent-executable', f)
if mode & 0o2:
need_verifyscript = True
printError(pkg, 'permissions-world-writable',
'%(file)s is packaged with world writable permissions (0%(mode)o)' %
{'file': f, 'mode': mode})
script = pkg[rpm.RPMTAG_POSTIN] or pkg.scriptprog(rpm.RPMTAG_POSTINPROG)
found = False
if script:
for line in script.split("\n"):
if "chkstat -n" in line and f in line:
found = True
break
if "SuSEconfig --module permissions" in line \
or "run_permissions is obsolete" in line:
found = True
found_suseconfig = True
break
if need_verifyscript and \
(f not in self.perms or 'static' not in self.perms[f]):
if not script or not found:
printError(pkg, 'permissions-missing-postin',
"missing %%set_permissions %s in %%post" % f)
need_set_permissions = True
script = pkg[rpm.RPMTAG_VERIFYSCRIPT] or pkg[rpm.RPMTAG_VERIFYSCRIPTPROG]
found = False
if script:
for line in script.split("\n"):
if "/chkstat" in line and f in line:
found = True
break
if not script or not found:
printWarning(pkg, 'permissions-missing-verifyscript',
"missing %%verify_permissions -e %s" % f)
if need_set_permissions:
if 'permissions' not in map(lambda x: x[0], pkg.prereq()):
printError(pkg, 'permissions-missing-requires',
"missing 'permissions' in PreReq")
if found_suseconfig:
printInfo(pkg, 'permissions-suseconfig-obsolete',
"%run_permissions is obsolete")
check = SUIDCheck()
AUDIT_BUG_URL = "https://en.opensuse.org/openSUSE:Package_security_guidelines#audit_bugs"
addDetails(
'permissions-unauthorized-file',
"""If the package is intended for inclusion in any SUSE product
please open a bug report to request review of the package by the
security team. Please refer to {} for more
information.""".format(AUDIT_BUG_URL),
'permissions-symlink',
"""permissions handling for symlinks is useless. Please contact
[email protected] to remove the entry. Please refer to {} for more
information.""".format(AUDIT_BUG_URL),
'permissions-dir-without-slash',
"""the entry in the permissions file refers to a directory. Please
contact [email protected] to append a slash to the entry in order to
avoid security problems. Please refer to {} for more information.""".format(AUDIT_BUG_URL),
'permissions-file-as-dir',
"""the entry in the permissions file refers to a directory but the
package actually contains a file. Please contact [email protected] to
remove the slash. Please refer to {} for more information.""".format(AUDIT_BUG_URL),
'permissions-incorrect',
"""please use the %attr macro to set the correct permissions.""",
'permissions-incorrect-owner',
"""please use the %attr macro to set the correct ownership.""",
'permissions-file-setuid-bit',
"""If the package is intended for inclusion in any SUSE product
please open a bug report to request review of the program by the
security team. Please refer to {} for more information.""".format(AUDIT_BUG_URL),
'permissions-directory-setuid-bit',
"""If the package is intended for inclusion in any SUSE product
please open a bug report to request review of the package by the
security team. Please refer to {} for more
information.""".format(AUDIT_BUG_URL),
'permissions-world-writable',
"""If the package is intended for inclusion in any SUSE product
please open a bug report to request review of the package by the
security team. Please refer to {} for more
information.""".format(AUDIT_BUG_URL),
'permissions-fscaps',
"""Packaging file capabilities is currently not supported. Please
use normal permissions instead. You may contact the security team to
request an entry that sets capabilities in /etc/permissions
instead.""",
'permissions-missing-postin',
"""Please add an appropriate %post section""",
'permissions-missing-requires',
"""Please add \"PreReq: permissions\"""",
'permissions-missing-verifyscript',
"""Please add a %verifyscript section""",
'permissions-suseconfig-obsolete',
"""The %run_permissions macro calls SuSEconfig which sets permissions for all
files in the system. Please use %set_permissions <filename> instead
to only set permissions for files contained in this package""",
)