FAT: Operating on broken FAT FS causes the write syscall to return negative number not equal to -1

From: Anatoly Trosinenko
Date: Sat Jul 14 2018 - 08:26:57 EST


How to reproduce:
1) Compile v4.18-rc4 kernel with the attached config
1) Unpack the attached FS image (128 Mb) and mount it as vfat to /mnt
2) Compile and run vfat-bug.c

What is expected:
`write` returns either -1 or small positive number.

What happens:
The -13619152 aka 0xffffffffff303030 is returned.


Best regards,
Anatoly

Attachment: config_v4.18-rc4
Description: Binary data

Attachment: vfat_128m.img.bz2
Description: Binary data

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

char buf[123];

int main() {
int fd = open("/mnt/abc", O_WRONLY, 0123);
for (int i = 0; i < sizeof(buf); ++i)
buf[i] = i + 1;
ssize_t res = write(fd, buf, sizeof(buf));
fprintf(stderr, "res = %ld %lx\n", res, res);
return 0;
}