-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuned-adm-safe-profiles.patch
317 lines (295 loc) · 9.24 KB
/
tuned-adm-safe-profiles.patch
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
bz #605217 - tuned-adm unsafe profiles loading
Fixes possible security issue allowing locally logged users
to execute any shell script with root permissions.
'tuned-adm' updated to the newest upstream version (0.2.14)
Author: Jan Vcelak <[email protected]>
Date: Thu, 17 Jun 2010 15:53:04 +0200
diff -urp tuned-0.2.11/tuned-adm tuned-0.2.11.fixed/tuned-adm
--- tuned-0.2.11/tuned-adm 2010-03-23 13:50:59.000000000 +0100
+++ tuned-0.2.11.fixed/tuned-adm 2010-06-17 15:43:12.189692545 +0200
@@ -24,47 +24,28 @@
import os
import sys
import locale
-import getopt
def usage():
print """
-Usage: tuned-adm [-p <profile_dir>] [-h] <command> [args]
-Options:
- -p <profile_dir> directory with profiles
- -h, --help show this help message and exit
+Usage: tuned-adm <command>
commands:
help show this help message and exit
list list all available and active profiles
active show current active profile
off switch off all tunning
- profile <profile> switch to given profile
+ profile <profile-name> switch to given profile
"""
if __name__ == "__main__":
-
- profile_dir = "/etc/tune-profiles/"
-
- try:
- opts, args = getopt.getopt(sys.argv[1:], "hp:", ["help"])
- except getopt.error, e:
- print >>sys.stderr, "Error parsing command-line arguments: %s" % e
- usage()
- sys.exit(1)
-
- for (opt, val) in opts:
- if opt in ['-h', "--help"]:
- usage()
- sys.exit(0)
- if opt in ['-p']:
- profile_dir = val
+ args = sys.argv[1:]
if len(args) < 1:
- print >>sys.stderr, ("Missing arguments.")
+ print >>sys.stderr, "Missing arguments."
usage()
sys.exit(1)
- if args[0] == "help":
+ if args[0] in [ "help", "--help", "-h" ]:
usage()
sys.exit(0)
@@ -73,10 +54,5 @@ if __name__ == "__main__":
sys.path.insert(0, TUNEDDIR)
from tuned_adm import tuned_adm
-
- tuned_adm.init(profile_dir)
- if (tuned_adm.run(args)):
- usage()
- sys.exit(1)
-
+ tuned_adm.run(args)
diff -urp tuned-0.2.11/tuned_adm.py tuned-0.2.11.fixed/tuned_adm.py
--- tuned-0.2.11/tuned_adm.py 2010-03-23 13:50:59.000000000 +0100
+++ tuned-0.2.11.fixed/tuned_adm.py 2010-06-17 15:42:20.605691150 +0200
@@ -16,106 +16,156 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
-import sys,os,glob
+import sys
+import os
+import os.path
+import glob
class Tuned_adm:
- def __init__(self):
- self.profile_dir = "/etc/tune-profiles/"
-
-
- def init(self, profile_dir):
- self.profile_dir = profile_dir
+ def __init__(self, profile_dir = "/etc/tune-profiles"):
+ self.profile_dir = os.path.normpath(profile_dir)
+ self.active_file = os.path.join(profile_dir, "active-profile")
+
+ def error(self, msg, exit_code = 1):
+ print >>sys.stderr, msg
+ sys.exit(exit_code)
+
+ def check_permissions(self):
+ if not os.getuid() == 0:
+ self.error("Only root can run this script.", 2)
def run(self, args):
- if args[0] == "list":
+ if args[0] == "list":
self.list()
elif args[0] == "active":
- self.active()
+ print self.get_active()
elif args[0] == "off":
- if not os.getuid()==0:
- print >>sys.stderr, "Only root can run this script";
- sys.exit(2)
+ self.check_permissions()
self.off()
elif args[0] == "profile":
- if not os.getuid()==0:
- print >>sys.stderr, "Only root can run this script";
- sys.exit(2)
- self.profile(args[1:])
+ if len(args) == 2:
+ self.check_permissions()
+ self.profile(args[1])
+ else:
+ self.error("Invalid profile specification. Use 'tuned-adm list' to get all available profiles.")
else:
- print >>sys.stderr, "Nonexistent argument %s" % args[0]
- return 1
-
+ self.error("Nonexistent argument '%s'." % args[0])
def list(self):
- if os.path.exists(self.profile_dir):
- modes = os.listdir(self.profile_dir)
- if len(modes) > 0:
- print "Modes: "
- for i in range(len(modes)):
- if modes[i] != "active-profile":
- print modes[i]
- else:
- print "No profiles defined."
- self.active()
+ active = self.get_active()
+ modes = os.listdir(self.profile_dir)
+ if len(modes) > 0:
+ print "Available profiles: "
+ for mode in modes:
+ dir = os.path.join(self.profile_dir, mode)
+ if not os.path.isdir(dir):
+ continue
+
+ if mode == active:
+ print "- %s (active)" % mode
+ else:
+ print "- %s" % mode
+ else:
+ print "No profiles defined."
- def active(self):
- profile = open(self.profile_dir+"/active-profile", "r").read()
- print "Current active profile: %s" % profile
+ def get_active(self):
+ file = open(self.active_file, "r")
+ profile = file.read()
+ file.close()
+ return profile
+
+ def set_active(self, profile):
+ file = open(self.active_file, "w")
+ file.write(profile)
+ file.close()
+
+ def verify_profile(self, profile):
+ path = os.path.abspath(os.path.join(self.profile_dir, profile))
+
+ if not path.startswith("%s/" % self.profile_dir):
+ return False
+
+ if not os.path.isdir(path):
+ return False
+
+ return True
+
+ def remove(self, wildcard, filter = None):
+ if filter == None:
+ filter = lambda file: True
+
+ files = glob.glob(wildcard)
+ for f in files:
+ if filter(f):
+ os.unlink(f)
def off(self):
- open(self.profile_dir+"/active-profile", "w").write("off")
- os.system("rm -rf /etc/ktune.d/*.conf")
- os.system("rm -rf /etc/ktune.d/*.sh")
+ self.set_active("off")
+
+ # remove profile settings
+ self.remove("/etc/ktune.d/*.conf", os.path.islink)
+ self.remove("/etc/ktune.d/*.sh", os.path.islink)
+
+ # restore previous ktune settings (if present)
if os.path.exists("/etc/sysconfig/ktune.bckp"):
os.rename("/etc/sysconfig/ktune.bckp", "/etc/sysconfig/ktune")
if os.path.exists("/etc/tuned.conf.bckp") and os.path.exists("/etc/tuned.conf"):
os.rename("/etc/tuned.conf.bckp", "/etc/tuned.conf")
+
+ # disable services
os.system('service ktune stop')
os.system('service tuned stop')
os.system('chkconfig --del ktune')
os.system('chkconfig --del tuned')
-
- def profile(self, args):
+ def profile(self, profile):
enablektune = False
enabletuned = False
- if len(args) == 0:
- print "No profile given. To list all available profiles please run:"
- print "tuned-adm list"
- return
- if not os.path.exists(self.profile_dir+"/"+args[0]):
- print "No profile with name %s found." % args[0]
- return
- if os.path.exists(self.profile_dir):
- os.system('service ktune stop')
- os.system('chkconfig --add ktune && chkconfig --level 345 ktune off')
- os.system('service tuned stop')
- os.system('chkconfig --add tuned && chkconfig --level 345 tuned off')
- modes = os.listdir(self.profile_dir)
- if modes > 0:
- print 'Switching to profile %s' % args[0]
- open(self.profile_dir+"/active-profile", "w").write(args[0])
- if os.path.exists('/etc/ktune.d/tunedadm.sh'):
- os.remove('/etc/ktune.d/tunedadm.sh')
- if os.path.exists('/etc/ktune.d/tunedadm.conf'):
- os.remove('/etc/ktune.d/tunedadm.conf')
- file = ('%s/%s/ktune.sysconfig' % (self.profile_dir, args[0]))
- if os.path.exists(file):
- enablektune = True
- os.rename('/etc/sysconfig/ktune', '/etc/sysconfig/ktune.bckp')
- os.symlink(file, '/etc/sysconfig/ktune')
- file = ('%s/%s/ktune.sh' % (self.profile_dir, args[0]))
- if os.path.exists(file):
- os.symlink(file, '/etc/ktune.d/tunedadm.sh')
- file = ('%s/%s/sysctl.ktune' % (self.profile_dir, args[0]))
- if os.path.exists(file):
- os.symlink(file, '/etc/ktune.d/tunedadm.conf')
- file = ('%s/%s/tuned.conf' % (self.profile_dir, args[0]))
- if os.path.exists(file):
- enabletuned = True
- os.rename('/etc/tuned.conf', '/etc/tuned.conf.bckp')
- os.symlink(file, '/etc/tuned.conf')
+
+ if not self.verify_profile(profile):
+ self.error("Invalid profile. Use 'tuned-adm list' to get all available profiles.")
+
+ profile_root = os.path.join(self.profile_dir, profile)
+
+ # disabling services
+
+ os.system('service ktune stop')
+ os.system('service tuned stop')
+ os.system('chkconfig --add ktune && chkconfig --level 345 ktune off')
+ os.system('chkconfig --add tuned && chkconfig --level 345 tuned off')
+
+ print >>sys.stderr, "Switching to profile '%s'" % profile
+ self.set_active(profile)
+
+ # ktune settings
+
+ self.remove('/etc/ktune.d/tunedadm.sh')
+ self.remove('/etc/ktune.d/tunedadm.conf')
+
+ file = os.path.join(profile_root, "ktune.sysconfig")
+ if os.path.isfile(file):
+ enablektune = True
+ os.rename('/etc/sysconfig/ktune', '/etc/sysconfig/ktune.bckp')
+ os.symlink(file, '/etc/sysconfig/ktune')
+
+ file = os.path.join(profile_root, 'ktune.sh')
+ if os.path.isfile(file):
+ os.symlink(file, '/etc/ktune.d/tunedadm.sh')
+
+ file = os.path.join(profile_root, 'sysctl.ktune')
+ if os.path.isfile(file):
+ os.symlink(file, '/etc/ktune.d/tunedadm.conf')
+
+ # tuned settings
+
+ file = os.path.join(profile_root, 'tuned.conf')
+ if os.path.isfile(file):
+ enabletuned = True
+ os.rename('/etc/tuned.conf', '/etc/tuned.conf.bckp')
+ os.symlink(file, '/etc/tuned.conf')
+
+ # enabling services
if enablektune:
os.system('service ktune start')
@@ -125,5 +175,4 @@ class Tuned_adm:
os.system('service tuned start')
os.system('chkconfig --add tuned && chkconfig --level 345 tuned on')
-
tuned_adm = Tuned_adm()