Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/envfs-core.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 * Copyright (c) 2019 Albert Schwarzkopf <a.schwarzkopf@phytec.de>, PHYTEC Messtechnik GmbH
4
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5
 */
6
7
/**
8
 * @file
9
 * @brief Environment handling support (host and target)
10
 *
11
 * Important: This file will also be used on the host to create
12
 * the default environment when building the barebox binary. So
13
 * do not add any new barebox related functions here!
14
 */
15
#define pr_fmt(fmt) "envfs: " fmt
16
17
#ifdef __BAREBOX__
18
#include <common.h>
19
#include <fs.h>
20
#include <crc.h>
21
#include <envfs.h>
22
#include <libbb.h>
23
#include <libgen.h>
24
#include <environment.h>
25
#include <libfile.h>
26
#else
27
#define pr_info(fmt, ...) printf(pr_fmt(fmt), ##__VA_ARGS__)
28
#define pr_warn(fmt, ...) printf(pr_fmt(fmt), ##__VA_ARGS__)
29
#endif
30
31
static int dir_remove_action(const char *filename, struct stat *statbuf,
32
    void *userdata, int depth)
33
3.74k
{
34
3.74k
  if (!depth)
35
192
    return 1;
36
37
3.55k
  rmdir(filename);
38
39
3.55k
  return 1;
40
3.74k
}
41
42
int envfs_check_super(struct envfs_super *super, size_t *size)
43
192
{
44
192
  if (ENVFS_32(super->magic) != ENVFS_MAGIC) {
45
0
    pr_info("no envfs (magic mismatch) - envfs never written?\n");
46
0
    return -EIO;
47
0
  }
48
49
192
  if (crc32(0, super, sizeof(*super) - 4) != ENVFS_32(super->sb_crc)) {
50
0
    pr_warn("wrong crc on env superblock\n");
51
0
    return -EIO;
52
0
  }
53
54
192
  if (super->major < ENVFS_MAJOR)
55
192
    pr_info("version %d.%d loaded into %d.%d\n",
56
192
      super->major, super->minor,
57
0
      ENVFS_MAJOR, ENVFS_MINOR);
58
59
192
  *size = ENVFS_32(super->size);
60
61
192
  return 0;
62
192
}
63
64
int envfs_check_data(struct envfs_super *super, const void *buf, size_t size)
65
192
{
66
192
  uint32_t crc;
67
68
192
  crc = crc32(0, buf, size);
69
192
  if (crc != ENVFS_32(super->crc)) {
70
0
    pr_warn("wrong crc on env\n");
71
0
    return -EIO;
72
0
  }
73
74
192
  return 0;
75
192
}
76
77
static int envfs_load_data_pass(struct envfs_super *super, void *buf, size_t size,
78
    const char *dir, unsigned int flags, bool dryrun)
79
384
{
80
384
  int fd, ret = 0;
81
384
  char *str, *tmp;
82
  /* for envfs < 1.0 */
83
384
  struct envfs_inode_end inode_end_dummy;
84
384
  struct stat s;
85
86
384
  inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO);
87
384
  inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC);
88
89
3.20k
  while (size) {
90
2.81k
    struct envfs_inode *inode;
91
2.81k
    struct envfs_inode_end *inode_end;
92
2.81k
    size_t inode_size, inode_headerlen, namelen, headerlen_full;
93
94
2.81k
    if (size < sizeof(*inode))
95
0
      goto invalid;
96
97
2.81k
    inode = buf;
98
2.81k
    buf += sizeof(struct envfs_inode);
99
2.81k
    size -= sizeof(struct envfs_inode);
100
101
2.81k
    if (ENVFS_32(inode->magic) != ENVFS_INODE_MAGIC) {
102
0
      pr_warn("wrong magic\n");
103
0
      ret = -EIO;
104
0
      goto out;
105
0
    }
106
2.81k
    inode_size = ENVFS_32(inode->size);
107
2.81k
    inode_headerlen = ENVFS_32(inode->headerlen);
108
109
    /* Bound the lengths before padding to avoid overflow on 32-bit. */
110
2.81k
    if (inode_headerlen > (size & ~(size_t)3))
111
0
      goto invalid;
112
2.81k
    headerlen_full = PAD4(inode_headerlen);
113
2.81k
    if (inode_size > ((size - headerlen_full) & ~(size_t)3))
114
0
      goto invalid;
115
116
2.81k
    namelen = strnlen(inode->data, inode_headerlen);
117
2.81k
    if (namelen == inode_headerlen)
118
0
      goto invalid;
119
2.81k
    namelen++;
120
121
2.81k
    if (super->major < 1)
122
0
      inode_end = &inode_end_dummy;
123
2.81k
    else {
124
2.81k
      if (PAD4(namelen) > inode_headerlen ||
125
2.81k
          inode_headerlen - PAD4(namelen) < sizeof(*inode_end))
126
0
        goto invalid;
127
2.81k
      inode_end = buf + PAD4(namelen);
128
2.81k
    }
129
130
2.81k
    buf += headerlen_full;
131
2.81k
    size -= headerlen_full;
132
133
2.81k
    if (ENVFS_32(inode_end->magic) != ENVFS_INODE_END_MAGIC) {
134
0
      printf("envfs: wrong inode_end_magic\n");
135
0
      ret = -EIO;
136
0
      goto out;
137
0
    }
138
139
2.81k
    if (S_ISLNK(ENVFS_32(inode_end->mode)) &&
140
0
        !memchr(buf, '\0', PAD4(inode_size)))
141
0
      goto invalid;
142
143
2.81k
    if (dryrun)
144
1.40k
      goto skip;
145
146
1.40k
    debug("loading %s size %zu namelen %zu headerlen %zu\n", inode->data,
147
1.40k
      inode_size, namelen, inode_headerlen);
148
149
1.40k
    str = concat_path_file(dir, inode->data);
150
151
1.40k
    tmp = strdup(str);
152
1.40k
    make_directory(dirname(tmp));
153
1.40k
    free(tmp);
154
155
1.40k
    ret = stat(str, &s);
156
1.40k
    if (!ret && (flags & ENV_FLAG_NO_OVERWRITE)) {
157
0
      printf("skip %s\n", str);
158
0
      goto skip;
159
0
    }
160
161
1.40k
    if (S_ISLNK(ENVFS_32(inode_end->mode))) {
162
0
      debug("symlink: %s -> %s\n", str, (char*)buf);
163
0
      if (!strcmp(buf, basename(str))) {
164
0
        unlink(str);
165
0
      } else {
166
0
        if (!ret)
167
0
          unlink(str);
168
169
0
        ret = symlink(buf, str);
170
0
        if (ret < 0)
171
0
          printf("symlink: %s -> %s : %s\n",
172
0
              str, (char*)buf, strerror(-errno));
173
0
      }
174
0
      free(str);
175
1.40k
    } else {
176
1.40k
      fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
177
1.40k
      free(str);
178
1.40k
      if (fd < 0) {
179
0
        printf("Open %m\n");
180
0
        ret = fd;
181
0
        goto out;
182
0
      }
183
184
1.40k
      ret = write(fd, buf, inode_size);
185
1.40k
      if (ret < inode_size) {
186
0
        perror("write");
187
0
        ret = -errno;
188
0
        close(fd);
189
0
        goto out;
190
0
      }
191
1.40k
      close(fd);
192
1.40k
    }
193
2.81k
skip:
194
2.81k
    buf += PAD4(inode_size);
195
2.81k
    size -= PAD4(inode_size);
196
2.81k
  }
197
198
384
  if (dryrun)
199
192
    return 0;
200
201
192
  recursive_action(dir, ACTION_RECURSE | ACTION_DEPTHFIRST, NULL,
202
192
      dir_remove_action, NULL, 0);
203
204
192
  ret = 0;
205
192
out:
206
192
  return ret;
207
0
invalid:
208
0
  pr_warn("malformed inode\n");
209
0
  return -EINVAL;
210
192
}
211
212
int envfs_load_data(struct envfs_super *super, void *buf, size_t size,
213
    const char *dir, unsigned int flags)
214
192
{
215
192
  int ret;
216
217
  /* Validate the entire image before making any filesystem changes. */
218
192
  ret = envfs_load_data_pass(super, buf, size, dir, flags, true);
219
192
  if (ret)
220
0
    return ret;
221
222
192
  return envfs_load_data_pass(super, buf, size, dir, flags, false);
223
192
}
224
225
int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags)
226
160
{
227
160
  int ret;
228
160
  size_t size;
229
160
  struct envfs_super *super = buf;
230
231
160
  if (len < 0 || (size_t)len < sizeof(*super))
232
0
    return -EINVAL;
233
234
160
  buf = super + 1;
235
236
160
  ret = envfs_check_super(super, &size);
237
160
  if (ret)
238
0
    return ret;
239
240
160
  if (size > (size_t)len - sizeof(*super))
241
0
    return -EINVAL;
242
243
160
  ret = envfs_check_data(super, buf, size);
244
160
  if (ret)
245
0
    return ret;
246
247
160
  ret = envfs_load_data(super, buf, size, dir, flags);
248
249
160
  return ret;
250
160
}