summaryrefslogtreecommitdiff
path: root/qutebrowser/misc/elf.py
blob: 4d44e56ac65120adac8a929c7e59b39e84bebaeb (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
322
323
324
325
326
327
328
329
330
331
332
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2021 Florian Bruhin (The-Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser 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 qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

"""Simplistic ELF parser to get the QtWebEngine/Chromium versions.

I know what you must be thinking when reading this: "Why on earth does qutebrowser have
an ELF parser?!". For one, because writing one was an interesting learning exercise. But
there's actually a reason it's here: QtWebEngine 5.15.x versions come with different
underlying Chromium versions, but there is no API to get the version of
QtWebEngine/Chromium...

We can instead:

a) Look at the Qt runtime version (qVersion()). This often doesn't actually correspond
to the QtWebEngine version (as that can be older/newer). Since there will be a
QtWebEngine 5.15.3 release, but not Qt itself (due to LTS licensing restrictions), this
isn't a reliable source of information.

b) Look at the PyQtWebEngine version (PyQt5.QtWebEngine.PYQT_WEBENGINE_VERSION_STR).
This is a good first guess (especially for our Windows/macOS releases), but still isn't
certain. Linux distributions often push a newer QtWebEngine before the corresponding
PyQtWebEngine release, and some (*cough* Gentoo *cough*) even publish QtWebEngine
"5.15.2" but upgrade the underlying Chromium.

c) Parse the user agent. This is what qutebrowser did before this monstrosity was
introduced (and still does as a fallback), but for some things (finding the proper
commandline arguments to pass) it's too late in the initialization process.

d) Spawn QtWebEngine in a subprocess and ask for its user-agent. This takes too long to
do it on every startup.

e) Ask the package manager for this information. This means we'd need to know (or guess)
the package manager and package name. Also see:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=752114

Because of all those issues, we instead look for the (fixed!) version string as part of
the user agent header. Because libQt5WebEngineCore is rather big (~120 MB), we don't
want to search through the entire file, so we instead have a simplistic ELF parser here
to find the .rodata section. This way, searching the version gets faster by some orders
of magnitudes (a couple of us instead of ms).

This is a "best effort" parser. If it errors out, we instead end up relying on the
PyQtWebEngine version, which is the next best thing.
"""

import struct
import enum
import re
import dataclasses
import mmap
import pathlib
from typing import IO, ClassVar, Dict, Optional, Tuple, cast

from PyQt5.QtCore import QLibraryInfo

from qutebrowser.utils import log, version


class ParseError(Exception):

    """Raised when the ELF file can't be parsed."""


class Bitness(enum.Enum):

    """Whether the ELF file is 32- or 64-bit."""

    x32 = 1
    x64 = 2


class Endianness(enum.Enum):

    """Whether the ELF file is little- or big-endian."""

    little = 1
    big = 2


def _unpack(fmt: str, fobj: IO[bytes]) -> Tuple:
    """Unpack the given struct format from the given file."""
    size = struct.calcsize(fmt)
    data = _safe_read(fobj, size)

    try:
        return struct.unpack(fmt, data)
    except struct.error as e:
        raise ParseError(e)


def _safe_read(fobj: IO[bytes], size: int) -> bytes:
    """Read from a file, handling possible exceptions."""
    try:
        return fobj.read(size)
    except (OSError, OverflowError) as e:
        raise ParseError(e)


def _safe_seek(fobj: IO[bytes], pos: int) -> None:
    """Seek in a file, handling possible exceptions."""
    try:
        fobj.seek(pos)
    except (OSError, OverflowError) as e:
        raise ParseError(e)


@dataclasses.dataclass
class Ident:

    """File identification for ELF.

    See https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
    (first 16 bytes).
    """

    magic: bytes
    klass: Bitness
    data: Endianness
    version: int
    osabi: int
    abiversion: int

    _FORMAT: ClassVar[str] = '<4sBBBBB7x'

    @classmethod
    def parse(cls, fobj: IO[bytes]) -> 'Ident':
        """Parse an ELF ident header from a file."""
        magic, klass, data, version, osabi, abiversion = _unpack(cls._FORMAT, fobj)

        try:
            bitness = Bitness(klass)
        except ValueError:
            raise ParseError(f"Invalid bitness {klass}")

        try:
            endianness = Endianness(data)
        except ValueError:
            raise ParseError(f"Invalid endianness {data}")

        return cls(magic, bitness, endianness, version, osabi, abiversion)


@dataclasses.dataclass
class Header:

    """ELF header without file identification.

    See https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
    (without the first 16 bytes).
    """

    typ: int
    machine: int
    version: int
    entry: int
    phoff: int
    shoff: int
    flags: int
    ehsize: int
    phentsize: int
    phnum: int
    shentsize: int
    shnum: int
    shstrndx: int

    _FORMATS: ClassVar[Dict[Bitness, str]] = {
        Bitness.x64: '<HHIQQQIHHHHHH',
        Bitness.x32: '<HHIIIIIHHHHHH',
    }

    @classmethod
    def parse(cls, fobj: IO[bytes], bitness: Bitness) -> 'Header':
        """Parse an ELF header from a file."""
        fmt = cls._FORMATS[bitness]
        return cls(*_unpack(fmt, fobj))


@dataclasses.dataclass
class SectionHeader:

    """ELF section header.

    See https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#Section_header
    """

    name: int
    typ: int
    flags: int
    addr: int
    offset: int
    size: int
    link: int
    info: int
    addralign: int
    entsize: int

    _FORMATS: ClassVar[Dict[Bitness, str]] = {
        Bitness.x64: '<IIQQQQIIQQ',
        Bitness.x32: '<IIIIIIIIII',
    }

    @classmethod
    def parse(cls, fobj: IO[bytes], bitness: Bitness) -> 'SectionHeader':
        """Parse an ELF section header from a file."""
        fmt = cls._FORMATS[bitness]
        return cls(*_unpack(fmt, fobj))


def get_rodata_header(f: IO[bytes]) -> SectionHeader:
    """Parse an ELF file and find the .rodata section header."""
    ident = Ident.parse(f)
    if ident.magic != b'\x7fELF':
        raise ParseError(f"Invalid magic {ident.magic!r}")

    if ident.data != Endianness.little:
        raise ParseError("Big endian is unsupported")

    if ident.version != 1:
        raise ParseError(f"Only version 1 is supported, not {ident.version}")

    header = Header.parse(f, bitness=ident.klass)

    # Read string table
    _safe_seek(f, header.shoff + header.shstrndx * header.shentsize)
    shstr = SectionHeader.parse(f, bitness=ident.klass)

    _safe_seek(f, shstr.offset)
    string_table = _safe_read(f, shstr.size)

    # Back to all sections
    for i in range(header.shnum):
        _safe_seek(f, header.shoff + i * header.shentsize)
        sh = SectionHeader.parse(f, bitness=ident.klass)
        name = string_table[sh.name:].split(b'\x00')[0]
        if name == b'.rodata':
            return sh

    raise ParseError("No .rodata section found")


@dataclasses.dataclass
class Versions:

    """The versions found in the ELF file."""

    webengine: str
    chromium: str


def _find_versions(data: bytes) -> Versions:
    """Find the version numbers in the given data.

    Note that 'data' can actually be a mmap.mmap, but typing doesn't handle that
    correctly: https://github.com/python/typeshed/issues/1467
    """
    match = re.search(
        br'QtWebEngine/([0-9.]+) Chrome/([0-9.]+)',
        data,
    )
    if match is None:
        raise ParseError("No match in .rodata")

    try:
        return Versions(
            webengine=match.group(1).decode('ascii'),
            chromium=match.group(2).decode('ascii'),
        )
    except UnicodeDecodeError as e:
        raise ParseError(e)


def _parse_from_file(f: IO[bytes]) -> Versions:
    """Parse the ELF file from the given path."""
    sh = get_rodata_header(f)

    rest = sh.offset % mmap.ALLOCATIONGRANULARITY
    mmap_offset = sh.offset - rest
    mmap_size = sh.size + rest

    try:
        with mmap.mmap(
            f.fileno(),
            mmap_size,
            offset=mmap_offset,
            access=mmap.ACCESS_READ,
        ) as mmap_data:
            return _find_versions(cast(bytes, mmap_data))
    except (OSError, OverflowError) as e:
        log.misc.debug(f"mmap failed ({e}), falling back to reading", exc_info=True)
        _safe_seek(f, sh.offset)
        data = _safe_read(f, sh.size)
        return _find_versions(data)


def parse_webenginecore() -> Optional[Versions]:
    """Parse the QtWebEngineCore library file."""
    if version.is_sandboxed():
        # Flatpak has Qt in /usr/lib/x86_64-linux-gnu, but QtWebEngine in /app/lib.
        library_path = pathlib.Path("/app/lib")
    else:
        library_path = pathlib.Path(QLibraryInfo.location(QLibraryInfo.LibrariesPath))

    # PyQt bundles those files with a .5 suffix
    lib_file = library_path / 'libQt5WebEngineCore.so.5'
    if not lib_file.exists():
        return None

    try:
        with lib_file.open('rb') as f:
            versions = _parse_from_file(f)

        log.misc.debug(f"Got versions from ELF: {versions}")
        return versions
    except ParseError as e:
        log.misc.debug(f"Failed to parse ELF: {e}", exc_info=True)
        return None