Skip to content

Commit f2c3c50

Browse files
committed
compose: Ensure we mmap only regular files
1 parent efdb05d commit f2c3c50

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/compose/table.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,31 +176,31 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
176176
return NULL;
177177

178178
path = get_xcomposefile_path(ctx);
179-
if (path) {
179+
if (path && is_regular_file(path)) {
180180
file = fopen(path, "rb");
181181
if (file)
182182
goto found_path;
183183
}
184184
free(path);
185185

186186
path = get_xdg_xcompose_file_path(ctx);
187-
if (path) {
187+
if (path && is_regular_file(path)) {
188188
file = fopen(path, "rb");
189189
if (file)
190190
goto found_path;
191191
}
192192
free(path);
193193

194194
path = get_home_xcompose_file_path(ctx);
195-
if (path) {
195+
if (path && is_regular_file(path)) {
196196
file = fopen(path, "rb");
197197
if (file)
198198
goto found_path;
199199
}
200200
free(path);
201201

202202
path = get_locale_compose_file_path(ctx, table->locale);
203-
if (path) {
203+
if (path && is_regular_file(path)) {
204204
file = fopen(path, "rb");
205205
if (file)
206206
goto found_path;

src/utils.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
#include "config.h"
2525

26+
#include <sys/stat.h>
2627
#include "utils.h"
2728

2829
#ifdef HAVE_MMAP
2930

3031
#include <fcntl.h>
3132
#include <unistd.h>
3233
#include <sys/mman.h>
33-
#include <sys/stat.h>
3434
#include <sys/types.h>
3535

3636
bool
@@ -111,6 +111,16 @@ unmap_file(char *str, size_t size)
111111

112112
#endif
113113

114+
bool
115+
is_regular_file(const char *path)
116+
{
117+
struct stat stat_buf;
118+
int err = stat(path, &stat_buf);
119+
if (err != 0) return false;
120+
121+
return S_ISREG(stat_buf.st_mode);
122+
}
123+
114124
// ASCII lower-case map.
115125
static const unsigned char lower_map[] = {
116126
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,

src/utils.h

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
# ifndef S_ISDIR
4848
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
4949
# endif
50+
# ifndef S_ISREG
51+
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
52+
# endif
5053
typedef SSIZE_T ssize_t;
5154
#endif
5255

@@ -263,6 +266,9 @@ check_eaccess(const char *path, int mode)
263266
return true;
264267
}
265268

269+
bool
270+
is_regular_file(const char *path);
271+
266272
#if defined(HAVE_SECURE_GETENV)
267273
# define secure_getenv secure_getenv
268274
#elif defined(HAVE___SECURE_GETENV)

test/compose.c

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "config.h"
2525
#include <time.h>
26+
#include <errno.h>
2627

2728
#include "xkbcommon/xkbcommon-compose.h"
2829

@@ -499,6 +500,18 @@ test_XCOMPOSEFILE(struct xkb_context *ctx)
499500
struct xkb_compose_table *table;
500501
char *path;
501502

503+
/* Error: directory */
504+
path = test_get_path("locale/en_US.UTF-8");
505+
setenv("XCOMPOSEFILE", path, 1);
506+
free(path);
507+
508+
table = xkb_compose_table_new_from_locale(ctx, "blabla",
509+
XKB_COMPOSE_COMPILE_NO_FLAGS);
510+
assert_printf(errno != ENODEV && errno != EISDIR,
511+
"Should not be an error from `map_file`\n");
512+
assert(!table);
513+
514+
/* OK: regular file */
502515
path = test_get_path("locale/en_US.UTF-8/Compose");
503516
setenv("XCOMPOSEFILE", path, 1);
504517
free(path);

0 commit comments

Comments
 (0)