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

mumble_plugin_linux.h « plugins - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cdb1b2e8b109db32879109fa687703c83e5d9ef (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
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
318
319
320
321
// Copyright 2005-2020 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_PLUGIN_LINUX_H_
#define MUMBLE_PLUGIN_LINUX_H_

#ifndef MUMBLE_PLUGIN_MAIN_H_
#	error "Include mumble_plugin_main.h instead of mumble_plugin_linux.h"
#endif

#include "mumble_plugin_utils.h"

#include <cstring>
#include <elf.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/uio.h>

// This function returns:
// -1 in case of failure.
// 0 if the process is 32-bit.
// 1 if the process is 64-bit.
static inline int isProcess64Bit(const procptr_t &baseAddress) {
	if (isWin32) {
		return isWin32Process64Bit(baseAddress);
	}

	// We can know the process architecture by looking at its ELF header.
	uint8_t elf[5];

	peekProc(baseAddress, elf, sizeof(elf));

	// The first 4 bytes constitute the magical number in ASCII: 0x7F 45 4c 46.
	if (!(elf[0] == 0x7f && elf[1] == 'E' && elf[2] == 'L' && elf[3] == 'F')) {
		return -1;
	}

	// The fifth byte is 1 in case the process is 32-bit or 2 in case it's 64-bit.
	return elf[4] != 1;
}

// This function returns:
// -1 in case of failure.
// 0 if the process is not running through Wine.
// 1 if the process is running through Wine.
static inline int8_t isProcessWin32(const procid_t &pid) {
	std::stringstream ss;
	ss << "/proc/";
	ss << static_cast< unsigned long >(pid);
	ss << "/exe";

	char *path = realpath(ss.str().c_str(), nullptr);
	if (!path) {
		return -1;
	}

	const char *filename = basename(path);

	if (strcmp(filename, "wine-preloader") == 0 || strcmp(filename, "wine64-preloader") == 0) {
		free(path);
		return 1;
	}

	// basename() returns a pointer to the basename's position in the string passed as argument.
	// For that reason we cannot free 'path' before the if statement.
	free(path);

	return 0;
}

static inline procptr_t getModuleAddr(const procid_t &pid, const wchar_t *modname) {
	std::wstring modnameWide(modname);
	std::string modnameNonWide(modnameWide.begin(), modnameWide.end());

	std::stringstream ss;
	ss << std::string("/proc/");
	ss << static_cast< unsigned long >(pid);
	ss << std::string("/maps");
	std::string mapsFn = ss.str();
	std::string maps   = readFile(mapsFn);

	if (maps.size() == 0) {
		return 0;
	}

	std::stringstream ssPath(maps);
	while (ssPath.good()) {
		std::string baseaddr;

		int ch;
		while (1) {
			ch = ssPath.get();
			if (ch == '-') {
				break;
			} else if (ch == EOF) {
				return 0;
			}
			baseaddr.push_back(static_cast< char >(ch));
		}

		// seek to perms
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to offset
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to dev
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to inode
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// seek to pathname
		do {
			ch = ssPath.get();
			if (ch == EOF) {
				return 0;
			}
		} while (ch != ' ');

		// eat spaces until we're at the beginning of pathname.
		while (ch == ' ') {
			if (ch == EOF) {
				return 0;
			}
			ch = ssPath.get();
		}
		ssPath.unget();

		std::string pathname;
		while (1) {
			ch = ssPath.get();
			if (ch == '\n') {
				break;
			} else if (ch == EOF) {
				return 0;
			}
			pathname.push_back(static_cast< char >(ch));
		};

		// OK, we found 'em!
		// Only treat path as a real path if it starts with /.
		if (pathname.size() > 0 && pathname.at(0) == '/') {
			// Find the basename.
			size_t lastSlash = pathname.find_last_of('/');
			if (pathname.size() > lastSlash + 1) {
				std::string basename = pathname.substr(lastSlash + 1);
				if (basename == modnameNonWide) {
					unsigned long addr = strtoul(baseaddr.c_str(), nullptr, 16);
					return addr;
				}
			}
		}
	}

	return 0;
}

static inline bool peekProc(const procptr_t &addr, void *dest, const size_t &len) {
	struct iovec in;
	in.iov_base = reinterpret_cast< void * >(addr); // Address from target process
	in.iov_len  = len;                              // Length

	struct iovec out;
	out.iov_base = dest;
	out.iov_len  = len;

	ssize_t nread = process_vm_readv(pPid, &out, 1, &in, 1, 0);

	return (nread != -1 && static_cast< size_t >(nread) == in.iov_len);
}

template< typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Dyn, typename Elf_Sym >
static inline procptr_t getExportedSymbolInternal(const std::string &symbol, const procptr_t module) {
	procptr_t hashTable = 0;
	procptr_t strTable  = 0;
	procptr_t symTable  = 0;

	const auto ehdr = peekProc< Elf_Ehdr >(module);
	const auto phdr = peekProcVector< Elf_Phdr >(module + ehdr.e_phoff, ehdr.e_phnum);

	for (size_t i = 0; i < phdr.size(); ++i) {
		if (phdr[i].p_type == PT_DYNAMIC) {
			const auto dyn = peekProcVector< Elf_Dyn >(module + phdr[i].p_vaddr, phdr[i].p_memsz / sizeof(Elf_Dyn));

			for (size_t j = 0; j < dyn.size(); ++j) {
				switch (dyn[j].d_tag) {
					case DT_HASH:
						hashTable = dyn[j].d_un.d_ptr;
						break;
					case DT_STRTAB:
						strTable = dyn[j].d_un.d_ptr;
						break;
					case DT_SYMTAB:
						symTable = dyn[j].d_un.d_ptr;
						break;
				}

				if (hashTable && strTable && symTable) {
					break;
				}
			}

			break;
		}
	}

	// Hash table pseudo-struct:
	// uint32_t nBucket;
	// uint32_t nChain;
	// uint32_t bucket[nBucket];
	// uint32_t chain[nChain];
	const auto nChain = peekProc< uint32_t >(hashTable + sizeof(uint32_t));

	for (uint32_t i = 0; i < nChain; ++i) {
		const auto sym  = peekProc< Elf_Sym >(symTable + sizeof(Elf_Sym) * i);
		const auto name = peekProcString(strTable + sym.st_name, symbol.size());

		if (name == symbol) {
			return module + sym.st_value;
		}
	}

	return 0;
}

static inline procptr_t getExportedSymbol(const std::string &symbol, const procptr_t module) {
	if (isWin32) {
		return getWin32ExportedSymbol(symbol, module);
	}

	if (is64Bit) {
		return getExportedSymbolInternal< Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, Elf64_Sym >(symbol, module);
	} else {
		return getExportedSymbolInternal< Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, Elf32_Sym >(symbol, module);
	}
}

static void generic_unlock() {
	pModule = 0;
	pPid    = 0;
}

static bool initialize(const std::multimap< std::wstring, unsigned long long int > &pids, const wchar_t *procname,
					   const wchar_t *modname = nullptr) {
	pModule = 0;

	if (!pids.empty()) {
		auto iter = pids.find(std::wstring(procname));
		if (iter != pids.end()) {
			pPid = static_cast< procid_t >(iter->second);
		} else {
			pPid = 0;
		}
	} else {
		pPid = 0;
	}

	if (!pPid) {
		return false;
	}

	pModule = getModuleAddr(procname);
	if (!pModule) {
		pPid = 0;
		return false;
	}

	int8_t ret = isProcessWin32(pPid);
	if (ret == -1) {
		generic_unlock();
		return false;
	}

	isWin32 = ret;

	ret = isProcess64Bit(pModule);
	if (ret == -1) {
		generic_unlock();
		return false;
	}

	is64Bit = ret;

	if (modname) {
		pModule = getModuleAddr(modname);
		if (!pModule) {
			pPid = 0;
			return false;
		}
	}

	return true;
}

#endif