blob: 0ae68b46dec8a44429d205311dda3901c2bd0155 (
plain)
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
|
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import sys
from vyos.utils.kernel import load_module
from vyos.utils.process import rc_cmd
def main() -> int:
if len(sys.argv) < 2:
# No value to validate
return 1
module = sys.argv[1].strip()
if not module:
return 1
# Keep the module name format strict.
if not re.fullmatch(r"[a-zA-Z0-9_\-]+", module):
return 1
# Ensure the module exists and is loadable (dry-run).
# This does not load the module.
try:
rc = load_module(module, quiet=True, dry_run=True)
except OSError:
return 1
if rc != 0:
return 1
# Validate that the module looks like a watchdog driver.
# Use modinfo filename location as the heuristic.
rc, out = rc_cmd(["modinfo", "-F", "filename", module])
if rc != 0:
return 1
filename = (out or "").strip().lower()
# Accept modules located under drivers/watchdog, plus explicit exception for
# ipmi_watchdog which lives in drivers/char/ipmi.
is_watchdog_driver = (
("/watchdog/" in filename)
or filename.endswith("/ipmi_watchdog.ko")
)
return 0 if is_watchdog_driver else 1
if __name__ == "__main__":
sys.exit(main())
|