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#ifdef __KLIBC__
28# define DO_DIRECT_MOUNT 1
29#else
30# define DO_DIRECT_MOUNT 0
31#endif
32
33#define _GNU_SOURCE
34#define _XOPEN_SOURCE 500
35#define _FILE_OFFSET_BITS 64
36#include <alloca.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <paths.h>
40#include <stdio.h>
41#include <string.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <inttypes.h>
45#include <sys/stat.h>
46#include <sys/types.h>
47#include <sys/wait.h>
48#include <sys/mount.h>
49
50#include "linuxioctl.h"
51
52#include <paths.h>
53#ifndef _PATH_MOUNT
54# define _PATH_MOUNT "/bin/mount"
55#endif
56#ifndef _PATH_UMOUNT
57# define _PATH_UMOUNT "/bin/umount"
58#endif
59#ifndef _PATH_TMP
60# define _PATH_TMP "/tmp/"
61#endif
62
63#include "syslinux.h"
64
65#if DO_DIRECT_MOUNT
66# include <linux/loop.h>
67#endif
68
69#include <getopt.h>
70#include <sysexits.h>
71#include "syslxcom.h"
72#include "setadv.h"
73#include "syslxopt.h"
74
75extern const char *program;
76
77pid_t mypid;
78char *mntpath = NULL;
79
80#if DO_DIRECT_MOUNT
81int loop_fd = -1;
82#endif
83
84void __attribute__ ((noreturn)) die(const char *msg)
85{
86 fprintf(stderr, "%s: %s\n", program, msg);
87
88#if DO_DIRECT_MOUNT
89 if (loop_fd != -1) {
90 ioctl(loop_fd, LOOP_CLR_FD, 0);
91 close(loop_fd);
92 loop_fd = -1;
93 }
94#endif
95
96 if (mntpath)
97 unlink(mntpath);
98
99 exit(1);
100}
101
102
103
104
105int do_mount(int dev_fd, int *cookie, const char *mntpath, const char *fstype)
106{
107 struct stat st;
108
109 (void)cookie;
110
111 if (fstat(dev_fd, &st) < 0)
112 return errno;
113
114#if DO_DIRECT_MOUNT
115 {
116 if (!S_ISBLK(st.st_mode)) {
117
118 unsigned int n = 0;
119 struct loop_info64 loopinfo;
120 int loop_fd;
121
122 for (n = 0; loop_fd < 0; n++) {
123 snprintf(devfdname, sizeof devfdname, "/dev/loop%u", n);
124 loop_fd = open(devfdname, O_RDWR);
125 if (loop_fd < 0 && errno == ENOENT) {
126 die("no available loopback device!");
127 }
128 if (ioctl(loop_fd, LOOP_SET_FD, (void *)dev_fd)) {
129 close(loop_fd);
130 loop_fd = -1;
131 if (errno != EBUSY)
132 die("cannot set up loopback device");
133 else
134 continue;
135 }
136
137 if (ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo) ||
138 (loopinfo.lo_offset = opt.offset,
139 ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo)))
140 die("cannot set up loopback device");
141 }
142
143 *cookie = loop_fd;
144 } else {
145 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
146 (unsigned long)mypid, dev_fd);
147 *cookie = -1;
148 }
149
150 return mount(devfdname, mntpath, fstype,
151 MS_NOEXEC | MS_NOSUID, "umask=077,quiet");
152 }
153#else
154 {
155 char devfdname[128], mnt_opts[128];
156 pid_t f, w;
157 int status;
158
159 snprintf(devfdname, sizeof devfdname, "/proc/%lu/fd/%d",
160 (unsigned long)mypid, dev_fd);
161
162 f = fork();
163 if (f < 0) {
164 return -1;
165 } else if (f == 0) {
166 if (!S_ISBLK(st.st_mode)) {
167 snprintf(mnt_opts, sizeof mnt_opts,
168 "rw,nodev,noexec,loop,offset=%llu,umask=077,quiet",
169 (unsigned long long)opt.offset);
170 } else {
171 snprintf(mnt_opts, sizeof mnt_opts,
172 "rw,nodev,noexec,umask=077,quiet");
173 }
174 execl(_PATH_MOUNT, _PATH_MOUNT, "-t", fstype, "-o", mnt_opts,
175 devfdname, mntpath, NULL);
176 _exit(255);
177 }
178
179 w = waitpid(f, &status, 0);
180 return (w != f || status) ? -1 : 0;
181 }
182#endif
183}
184
185
186
187
188void do_umount(const char *mntpath, int cookie)
189{
190#if DO_DIRECT_MOUNT
191 int loop_fd = cookie;
192
193 if (umount2(mntpath, 0))
194 die("could not umount path");
195
196 if (loop_fd != -1) {
197 ioctl(loop_fd, LOOP_CLR_FD, 0);
198 close(loop_fd);
199 loop_fd = -1;
200 }
201#else
202 pid_t f = fork();
203 pid_t w;
204 int status;
205 (void)cookie;
206
207 if (f < 0) {
208 perror("fork");
209 exit(1);
210 } else if (f == 0) {
211 execl(_PATH_UMOUNT, _PATH_UMOUNT, mntpath, NULL);
212 }
213
214 w = waitpid(f, &status, 0);
215 if (w != f || status) {
216 exit(1);
217 }
218#endif
219}
220
221
222
223
224int modify_existing_adv(const char *path)
225{
226 if (opt.reset_adv)
227 syslinux_reset_adv(syslinux_adv);
228 else if (read_adv(path, "ldlinux.sys") < 0)
229 return 1;
230
231 if (modify_adv() < 0)
232 return 1;
233
234 if (write_adv(path, "ldlinux.sys") < 0)
235 return 1;
236
237 return 0;
238}
239
240int main(int argc, char *argv[])
241{
242 static unsigned char sectbuf[SECTOR_SIZE];
243 int dev_fd, fd;
244 struct stat st;
245 int err = 0;
246 char mntname[128];
247 char *ldlinux_name;
248 char *ldlinux_path;
249 char *subdir;
250 sector_t *sectors = NULL;
251 int ldlinux_sectors = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
252 const char *errmsg;
253 int mnt_cookie;
254 int patch_sectors;
255 int i;
256
257 mypid = getpid();
258 umask(077);
259 parse_options(argc, argv, MODE_SYSLINUX);
260
261
262 if (opt.directory && opt.directory[0]) {
263 int len = strlen(opt.directory);
264 int rv = asprintf(&subdir, "%s%s%s",
265 opt.directory[0] == '/' ? "" : "/",
266 opt.directory,
267 opt.directory[len-1] == '/' ? "" : "/");
268 if (rv < 0 || !subdir) {
269 perror(program);
270 exit(1);
271 }
272 } else {
273 subdir = "/";
274 }
275
276 if (!opt.device || opt.install_mbr || opt.activate_partition)
277 usage(EX_USAGE, MODE_SYSLINUX);
278
279
280
281
282
283 dev_fd = open(opt.device, O_RDWR);
284 if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
285 perror(opt.device);
286 exit(1);
287 }
288
289 if (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
290 die("not a device or regular file");
291 }
292
293 if (opt.offset && S_ISBLK(st.st_mode)) {
294 die("can't combine an offset with a block device");
295 }
296
297 fs_type = VFAT;
298 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
299 fsync(dev_fd);
300
301
302
303
304 if ((errmsg = syslinux_check_bootsect(sectbuf))) {
305 fprintf(stderr, "%s: %s\n", opt.device, errmsg);
306 exit(1);
307 }
308
309
310
311
312 if (geteuid()) {
313 die("This program needs root privilege");
314 } else {
315 int i = 0;
316 struct stat dst;
317 int rv;
318
319
320
321
322 if (chdir(_PATH_TMP)) {
323 fprintf(stderr, "%s: Cannot access the %s directory.\n",
324 program, _PATH_TMP);
325 exit(1);
326 }
327#define TMP_MODE (S_IXUSR|S_IWUSR|S_IXGRP|S_IWGRP|S_IWOTH|S_IXOTH|S_ISVTX)
328
329 if (stat(".", &dst) || !S_ISDIR(dst.st_mode) ||
330 (dst.st_mode & TMP_MODE) != TMP_MODE) {
331 die("possibly unsafe " _PATH_TMP " permissions");
332 }
333
334 for (i = 0;; i++) {
335 snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
336 (unsigned long)mypid, i);
337
338 if (lstat(mntname, &dst) != -1 || errno != ENOENT)
339 continue;
340
341 rv = mkdir(mntname, 0000);
342
343 if (rv == -1) {
344 if (errno == EEXIST || errno == EINTR)
345 continue;
346 perror(program);
347 exit(1);
348 }
349
350 if (lstat(mntname, &dst) || dst.st_mode != (S_IFDIR | 0000) ||
351 dst.st_uid != 0) {
352 die("someone is trying to symlink race us!");
353 }
354 break;
355 }
356
357 mntpath = mntname;
358 }
359
360 if (do_mount(dev_fd, &mnt_cookie, mntpath, "vfat") &&
361 do_mount(dev_fd, &mnt_cookie, mntpath, "msdos")) {
362 rmdir(mntpath);
363 die("mount failed");
364 }
365
366 ldlinux_path = alloca(strlen(mntpath) + strlen(subdir) + 1);
367 sprintf(ldlinux_path, "%s%s", mntpath, subdir);
368
369 ldlinux_name = alloca(strlen(ldlinux_path) + 14);
370 if (!ldlinux_name) {
371 perror(program);
372 err = 1;
373 goto umount;
374 }
375 sprintf(ldlinux_name, "%sldlinux.sys", ldlinux_path);
376
377
378 if (opt.update_only == -1) {
379 if (opt.reset_adv || opt.set_once) {
380 modify_existing_adv(ldlinux_path);
381 do_umount(mntpath, mnt_cookie);
382 sync();
383 rmdir(mntpath);
384 exit(0);
385 } else {
386 fprintf(stderr, "%s: please specify --install or --update for the future\n", argv[0]);
387 opt.update_only = 0;
388 }
389 }
390
391
392 if (opt.reset_adv)
393 syslinux_reset_adv(syslinux_adv);
394 else if (read_adv(ldlinux_path, "ldlinux.sys") < 0)
395 syslinux_reset_adv(syslinux_adv);
396 if (modify_adv() < 0)
397 exit(1);
398
399 if ((fd = open(ldlinux_name, O_RDONLY)) >= 0) {
400 uint32_t zero_attr = 0;
401 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &zero_attr);
402 close(fd);
403 }
404
405 unlink(ldlinux_name);
406 fd = open(ldlinux_name, O_WRONLY | O_CREAT | O_TRUNC, 0444);
407 if (fd < 0) {
408 perror(opt.device);
409 err = 1;
410 goto umount;
411 }
412
413
414 if (xpwrite(fd, boot_image, boot_image_len, 0) != (int)boot_image_len ||
415 xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
416 boot_image_len) != 2 * ADV_SIZE) {
417 fprintf(stderr, "%s: write failure on %s\n", program, ldlinux_name);
418 exit(1);
419 }
420
421 fsync(fd);
422
423
424
425 {
426 uint32_t attr = 0x07;
427 ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
428 }
429
430
431
432
433 ldlinux_sectors += 2;
434 sectors = calloc(ldlinux_sectors, sizeof *sectors);
435 if (sectmap(fd, sectors, ldlinux_sectors)) {
436 perror("bmap");
437 exit(1);
438 }
439 close(fd);
440 sync();
441
442umount:
443 do_umount(mntpath, mnt_cookie);
444 sync();
445 rmdir(mntpath);
446
447 if (err)
448 exit(err);
449
450
451
452
453 i = syslinux_patch(sectors, ldlinux_sectors, opt.stupid_mode,
454 opt.raid_mode, subdir, NULL);
455 patch_sectors = (i + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
456
457
458
459
460 for (i = 0; i < patch_sectors; i++) {
461 xpwrite(dev_fd, boot_image + i * SECTOR_SIZE, SECTOR_SIZE,
462 opt.offset + ((off_t) sectors[i] << SECTOR_SHIFT));
463 }
464
465
466
467
468
469
470 xpread(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
471
472
473 syslinux_make_bootsect(sectbuf);
474
475
476 xpwrite(dev_fd, sectbuf, SECTOR_SIZE, opt.offset);
477
478 close(dev_fd);
479 sync();
480
481
482
483 return 0;
484}
485