Debian Codegraph @

tapset/livepatch.stp

raw
1 // livepatch tapset macros
2 // Copyright (C) 2022 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8 private cve_id
9 private cve_load_time
10
11 global cve_notify_p = 1
12 global cve_fix_p = 1
13 global cve_trace_p = 1
14 global cve_enabled_p = 1
15 global cve_tmpdisabled_s = -1
16
17 probe begin {
18 cve_id = module_name()
19 cve_load_time = gettimeofday_s()
20
21 if (cve_notify_p)
22 printf("%s mitigation loaded\n", cve_id)
23 }
24
25 probe end,error {
26 if (cve_notify_p)
27 printf("%s mitigation unloaded\n", cve_id)
28 }
29
30 # Parameter reading and updating
31 @define cve_probe_procfs_parameter (glbl, f_name) %(
32 probe procfs(@f_name).read
33 { $value = sprintf("%d\n", @glbl) }
34
35 probe procfs(@f_name).write
36 { @glbl = strtol($value, 10) }
37 %)
38
39 @cve_probe_procfs_parameter(cve_notify_p , "cve_notify_p")
40 @cve_probe_procfs_parameter(cve_trace_p , "cve_trace_p")
41 @cve_probe_procfs_parameter(cve_fix_p , "cve_fix_p")
42 @cve_probe_procfs_parameter(cve_enabled_p , "cve_enabled_p")
43
44 probe procfs("cve_tmpdisabled_s").read
45 { $value = sprintf("%d\n", cve_tmpdisabled_s) }
46 probe procfs("cve_tmpdisabled_s").write
47 { cve_tmpdisabled_s = strtol($value, 10)
48 if (cve_tmpdisabled_s > 0)
49 cve_enabled_p = 0 # trigger temporary disable
50 }
51
52
53 # The metric accumulation and prometheus
54 global cve_metrics
55
56 /**
57 * sfunction cve_count_metric - Increment the count of key
58 *
59 * @key: The metric
60 *
61 * Description: This function increments the count of the metric
62 * key by 1. The metrics can be accessed in
63 * /proc/systemtap/MODULE_NAME/__prometheus
64 */
65 function cve_count_metric (key:string) {
66 cve_metrics[key] ++
67 }
68
69 /**
70 * sfunction cve_record_metric - Set the value of key
71 *
72 * @key: The metric
73 * @value: The new value
74 *
75 * Description: This function sets the value of the metric
76 * key. The metrics can be accessed in
77 * /proc/systemtap/MODULE_NAME/__prometheus
78 */
79 function cve_record_metric (key:string, value:long) {
80 cve_metrics[key] = value
81 }
82
83 probe prometheus {
84 cve_record_metric("runtime", gettimeofday_s() - cve_load_time)
85 @prometheus_dump_array1(cve_metrics, cve_id, "metric")
86 }
87
88 # Disabling the patch
89 probe timer.s(1) {
90 if(!cve_enabled_p && cve_tmpdisabled_s >= 0){
91 if(cve_tmpdisabled_s > 0){
92 cve_tmpdisabled_s--
93 }else{
94 if (cve_notify_p)
95 printf("%s reenabling\n", cve_id)
96 cve_enabled_p = 1
97 cve_tmpdisabled_s = -1
98 }
99 }
100 }
101
102 /**
103 * sfunction cve_tmpdisable - Disable the cve livepatch
104 *
105 * @duration: The number of seconds to disable
106 *
107 * Description: This function temporarily disables the
108 * conditionals which use cve_enabled_p for duration seconds.
109 * If duration is -1, disable the livepatch until reenabled.
110 */
111 function cve_tmpdisable(duration:long){
112 if (cve_notify_p)
113 printf("%s disabling temporarily\n", cve_id)
114 cve_enabled_p = 0
115 cve_tmpdisabled_s = duration
116 }