off_t size; /* in bytes */
};
+static bool is_open(int fd)
+{
+ return fcntl(fd, F_GETFD) != -1;
+}
+
static int open_ro(const char* spec)
{
return open(spec, O_RDONLY);
struct exfat_dev* dev;
struct stat stbuf;
+ /* The system allocates file descriptors sequentially. If we have been
+ started with stdin (0), stdout (1) or stderr (2) closed, the system
+ will give us descriptor 0, 1 or 2 later when we open block device,
+ FUSE communication pipe, etc. As a result, functions using stdin,
+ stdout or stderr will actualy work with a different thing and can
+ corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
+ while (!is_open(STDIN_FILENO)
+ || !is_open(STDOUT_FILENO)
+ || !is_open(STDERR_FILENO))
+ {
+ /* we don't need those descriptors, let them leak */
+ if (open("/dev/null", O_RDWR) == -1)
+ {
+ exfat_error("failed to open /dev/null");
+ return NULL;
+ }
+ }
+
dev = malloc(sizeof(struct exfat_dev));
if (dev == NULL)
{