Some weird things about memory access in Linux

Zheng Yu lucky

In-field Overflow Access

In lib/xz/xz_dec_lzma2.c, we can find the following code:

1
2
3
4
5
6
7
8
9
10
static void lzma_reset(struct xz_dec_lzma2 *s)
{
uint16_t *probs;
// ...
probs = s->lzma.is_match[0];
for (i = 0; i < PROBS_TOTAL; ++i)
probs[i] = RC_BIT_MODEL_TOTAL / 2;

// ...
}

We note that PROBS_TOTAL (14133) is larger than POS_STATES_MAX (16), but it is a legal access for lzma.is_match because the program want to set all the tail elements in lzma_dec.

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
struct lzma_dec {
// ...

/* If 1, it's a match. Otherwise it's a single 8-bit literal. */
uint16_t is_match[STATES][POS_STATES_MAX];

/* If 1, it's a match. Otherwise it's a single 8-bit literal. */
uint16_t is_match[STATES][POS_STATES_MAX];

/* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
uint16_t is_rep[STATES];

/*
* If 0, distance of a repeated match is rep0.
* Otherwise check is_rep1.
*/
uint16_t is_rep0[STATES];

/*
* If 0, distance of a repeated match is rep1.
* Otherwise check is_rep2.
*/
uint16_t is_rep1[STATES];

/* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
uint16_t is_rep2[STATES];

/*
* If 1, the repeated match has length of one byte. Otherwise
* the length is decoded from rep_len_decoder.
*/
uint16_t is_rep0_long[STATES][POS_STATES_MAX];

/*
* Probability tree for the highest two bits of the match
* distance. There is a separate probability tree for match
* lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
*/
uint16_t dist_slot[DIST_STATES][DIST_SLOTS];

/*
* Probility trees for additional bits for match distance
* when the distance is in the range [4, 127].
*/
uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];

/*
* Probability tree for the lowest four bits of a match
* distance that is equal to or greater than 128.
*/
uint16_t dist_align[ALIGN_SIZE];

/* Length of a normal match */
struct lzma_len_dec match_len_dec;

/* Length of a repeated match */
struct lzma_len_dec rep_len_dec;

/* Probabilities of literals */
uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
};
  • Post title:Some weird things about memory access in Linux
  • Post author:Zheng Yu
  • Create time:2023-04-16 14:34:20
  • Post link:https://dataisland.org/2023/04/16/mem-access-in-linux/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
On this page
Some weird things about memory access in Linux