Welcome to mirror list, hosted at ThFree Co, Russian Federation.

shutdown_rarload_01042.patch « clamav « patches « lib - github.com/lavabit/magma.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ec7fdbf088adcb3bd0d3450b6b7e98825552594 (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
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
diff --git a/libclamav/clamav.h b/libclamav/clamav.h
index d4be07e..70e9c72 100644
--- a/libclamav/clamav.h
+++ b/libclamav/clamav.h
@@ -265,6 +265,7 @@ void cl_cleanup_crypto(void);
  * @return cl_error_t   CL_SUCCESS if everything initalized correctly.
  */
 extern cl_error_t cl_init(unsigned int initoptions);
+extern void cl_shutdown(void);
 
 /**
  * @brief Allocate a new scanning engine and initialize default settings.
diff --git a/libclamav/mbox.c b/libclamav/mbox.c
index 1f1e1c3..ac97d18 100644
--- a/libclamav/mbox.c
+++ b/libclamav/mbox.c
@@ -3052,6 +3052,30 @@ initialiseTables(table_t **rfc821Table, table_t **subtypeTable)
 }
 
 /*
+ * Cleanup the various lookup tables
+ */
+void
+cli_mbox_shutdown(void)
+{
+
+#ifdef	CL_THREAD_SAFE
+	pthread_mutex_lock(&tables_mutex);
+#endif
+	if(rfc821) {
+		tableDestroy(rfc821);
+		rfc821 = NULL;
+	}
+	if(subtype) {
+		tableDestroy(subtype);
+		subtype = NULL;
+	}
+#ifdef	CL_THREAD_SAFE
+			pthread_mutex_unlock(&tables_mutex);
+#endif
+
+}
+
+/*
  * If there's a HTML text version use that, otherwise
  * use the first text part, otherwise just use the
  * first one around. HTML text is most likely to include
diff --git a/libclamav/mbox.h b/libclamav/mbox.h
index 63ee0c4..fb767f5 100644
--- a/libclamav/mbox.h
+++ b/libclamav/mbox.h
@@ -75,4 +75,6 @@ typedef enum {
 size_t strstrip(char *s); /* remove trailing white space */
 int cli_mbox(const char *dir, cli_ctx *ctx);
 
+void cli_mbox_shutdown(void);
+
 #endif /* __MBOX_H */
diff --git a/libclamav/message.c b/libclamav/message.c
index 75c86ec..cc2fb7b 100644
--- a/libclamav/message.c
+++ b/libclamav/message.c
@@ -137,6 +137,32 @@ static const unsigned char base64Table[256] = {
     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
     255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
 
+static table_t *mime_table = NULL;
+
+#ifdef	CL_THREAD_SAFE
+	static pthread_mutex_t mime_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
+/*
+ * Cleanup the various lookup tables
+ */
+void
+cli_mime_shutdown(void)
+{
+
+#ifdef	CL_THREAD_SAFE
+	pthread_mutex_lock(&mime_mutex);
+#endif
+	if(mime_table) {
+		tableDestroy(mime_table);
+		mime_table = NULL;
+	}
+#ifdef	CL_THREAD_SAFE
+			pthread_mutex_unlock(&mime_mutex);
+#endif
+
+}
+
 message *
 messageCreate(void)
 {
@@ -211,12 +237,8 @@ void messageReset(message *m)
  */
 int messageSetMimeType(message *mess, const char *type)
 {
-#ifdef CL_THREAD_SAFE
-    static pthread_mutex_t mime_mutex = PTHREAD_MUTEX_INITIALIZER;
-#endif
     const struct mime_map *m;
     int typeval;
-    static table_t *mime_table;
 
     if (mess == NULL) {
         cli_dbgmsg("messageSetMimeType: NULL message pointer\n");
diff --git a/libclamav/message.h b/libclamav/message.h
index 62f6b85..a0ec5cd 100644
--- a/libclamav/message.h
+++ b/libclamav/message.h
@@ -93,4 +93,6 @@ int messageSavePartial(message *m, const char *dir, const char *id, unsigned par
 json_object *messageGetJObj(message *m);
 #endif
 
+void cli_mime_shutdown(void);
+
 #endif /*_MESSAGE_H*/
diff --git a/libclamav/others.c b/libclamav/others.c
index 4c31381..f3a9c3b 100644
--- a/libclamav/others.c
+++ b/libclamav/others.c
@@ -268,7 +268,9 @@ static void *get_module_function(void *handle, const char *name)
 {
     void *procAddress = NULL;
     procAddress = dlsym(handle, name);
-    if (NULL == procAddress) {
+
+    // Ignore missing symbols when the handle is NULL. This only means the symbol wasn't statically linked or otherwise loaded already.
+    if (NULL == procAddress && handle != NULL) {
         const char *err = dlerror();
         if (NULL == err) {
             cli_warnmsg("Failed to get function \"%s\": Unknown error.\n", name);
@@ -276,6 +278,10 @@ static void *get_module_function(void *handle, const char *name)
             cli_warnmsg("Failed to get function \"%s\": %s\n", name, err);
         }
     }
+    // If the symbol wasn't found, and the handle is NULL, this will call dlerror(), which will reset the error queue.
+    else if (NULL == procAddress && handle == NULL) {
+    	dlerror();
+    }
     return procAddress;
 }
 #endif // !_WIN32
@@ -302,20 +308,26 @@ static void rarload(void)
     cli_unrar_skip_file        = unrar_skip_file;
     cli_unrar_close            = unrar_close;
 #else
-    rhandle = load_module("libclamunrar_iface", "unrar");
-    if (NULL == rhandle)
-        return;
 
-    if ((NULL == (cli_unrar_open = (cl_unrar_error_t(*)(const char *, void **, char **, uint32_t *, uint8_t))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_open"))) ||
-        (NULL == (cli_unrar_peek_file_header = (cl_unrar_error_t(*)(void *, unrar_metadata_t *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_peek_file_header"))) ||
-        (NULL == (cli_unrar_extract_file = (cl_unrar_error_t(*)(void *, const char *, char *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_extract_file"))) ||
-        (NULL == (cli_unrar_skip_file = (cl_unrar_error_t(*)(void *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_skip_file"))) ||
-        (NULL == (cli_unrar_close = (void (*)(void *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_close")))) {
-
-        cli_warnmsg("Failed to load function from UnRAR module\n");
-        cli_warnmsg("Version mismatch?\n");
-        cli_warnmsg("UnRAR support unavailable\n");
-        return;
+    if ((NULL == (cli_unrar_open = (cl_unrar_error_t(*)(const char *, void **, char **, uint32_t *, uint8_t))get_module_function(NULL, "libclamunrar_iface_LTX_unrar_open"))) ||
+        (NULL == (cli_unrar_peek_file_header = (cl_unrar_error_t(*)(void *, unrar_metadata_t *))get_module_function(NULL, "libclamunrar_iface_LTX_unrar_peek_file_header"))) ||
+        (NULL == (cli_unrar_extract_file = (cl_unrar_error_t(*)(void *, const char *, char *))get_module_function(NULL, "libclamunrar_iface_LTX_unrar_extract_file"))) ||
+        (NULL == (cli_unrar_skip_file = (cl_unrar_error_t(*)(void *))get_module_function(NULL, "libclamunrar_iface_LTX_unrar_skip_file"))) ||
+        (NULL == (cli_unrar_close = (void (*)(void *))get_module_function(NULL, "libclamunrar_iface_LTX_unrar_close")))) {
+
+	    rhandle = load_module("libclamunrar_iface", "unrar");
+	    if (NULL == rhandle) return;
+
+		if ((NULL == (cli_unrar_open = (cl_unrar_error_t(*)(const char *, void **, char **, uint32_t *, uint8_t))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_open"))) ||
+				(NULL == (cli_unrar_peek_file_header = (cl_unrar_error_t(*)(void *, unrar_metadata_t *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_peek_file_header"))) ||
+				(NULL == (cli_unrar_extract_file = (cl_unrar_error_t(*)(void *, const char *, char *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_extract_file"))) ||
+				(NULL == (cli_unrar_skip_file = (cl_unrar_error_t(*)(void *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_skip_file"))) ||
+				(NULL == (cli_unrar_close = (void (*)(void *))get_module_function(rhandle, "libclamunrar_iface_LTX_unrar_close")))) {
+		    cli_warnmsg("Failed to load function from UnRAR module\n");
+		    cli_warnmsg("Version mismatch?\n");
+		    cli_warnmsg("UnRAR support unavailable\n");
+		    return;
+		}
     }
 #endif
 
@@ -440,6 +452,18 @@ cl_error_t cl_init(unsigned int initoptions)
     return CL_SUCCESS;
 }
 
+void cl_shutdown(void) {
+	cli_mbox_shutdown();
+	cli_mime_shutdown();
+	// Uncomment this fix once we patch libltdl to let us check the initialized counter. Until then use the ugly warning below as a reminder.
+	// if (lt_dlinitialized() && lt_dlexit()) {
+	// 	cli_errmsg("lt_dlexit: Library exit error, probably because of an invalid reference counter");
+	// }
+	if (lt_dlexit()) {
+		cli_errmsg("lt_dlexit: Library exit error, probably because of an invalid reference counter [LIBLTDL NO LONGER BUNDLED / OTHERWISE WE'D USE 0.103.5 PATCH TO CHECK INITALIZED]");
+	}
+}
+
 struct cl_engine *cl_engine_new(void)
 {
     struct cl_engine *new;