Repository navigation
Commit 8e3306e
lxutil: keep O_TRUNC from making a Windows handle writable
On Windows, LxVolume::open asked for FILE_WRITE_DATA whenever the open
flags held O_TRUNC, since Windows truncates only through a handle with
write access, and the LxFile then kept that handle, and recorded that
access, for its lifetime. So the file that an O_RDONLY | O_TRUNC or
O_NOACCESS | O_TRUNC open returned could be written with pwrite and
truncated with set_attr, unlike a Linux descriptor with the same flags,
which fails such a write with EBADF and such a truncation with EINVAL;
and O_APPEND | O_TRUNC got FILE_WRITE_DATA back, so its writes went to
their offsets rather than to the end of the file. A virtio-fs guest
could thus keep writing a file through a handle that it had opened
read-only (microsoft/nvx#1609), and a microVM restore, which reopens
the handle without O_TRUNC, silently took that access away.
open_flags_to_access_mask now derives the access of the open file from
its access mode and O_APPEND alone. LxVolume::open truncates an
existing file through the file's handle when that access includes
FILE_WRITE_DATA, and otherwise through a handle reopened with
FILE_WRITE_DATA for the truncation alone, which it then closes. The
open still needs write access to truncate, as on Linux, and fails with
EACCES without it.
truncating_open_keeps_the_access_mode opens an existing file with
O_RDONLY | O_TRUNC and with O_NOACCESS | O_TRUNC, and creates one with
O_RDONLY | O_CREAT | O_TRUNC, and checks that each open leaves the file
empty but that a pwrite through it fails with EBADF and a truncate with
EINVAL. truncating_append_open_stays_append_only checks that two
writes at offset 0 through O_WRONLY | O_APPEND, with and without
O_TRUNC, both append. Without the fix, at feature/microvm/dev b51f915
on Windows, the pwrite through the O_RDONLY | O_TRUNC file succeeds,
and the writes through the O_WRONLY | O_APPEND | O_TRUNC one overwrite
each other; with it, both tests and the package's others pass there.
Both pass on Linux (Azure Linux 3.0, nvx-kvm-test) with and without
the fix, since the host kernel gives a descriptor these semantics.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent b1c57d5 commit 8e3306e
3 files changed
Lines changed: 65 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1452 | 1452 | | |
1453 | 1453 | | |
1454 | 1454 | | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
1455 | 1511 | | |
1456 | 1512 | | |
1457 | 1513 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
326 | 328 | | |
327 | | - | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
328 | 334 | | |
329 | 335 | | |
330 | 336 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
501 | 501 | | |
502 | 502 | | |
503 | 503 | | |
504 | | - | |
| 504 | + | |
505 | 505 | | |
506 | 506 | | |
507 | 507 | | |
| |||
516 | 516 | | |
517 | 517 | | |
518 | 518 | | |
519 | | - | |
520 | | - | |
521 | | - | |
522 | | - | |
523 | | - | |
524 | 519 | | |
525 | 520 | | |
526 | 521 | | |
| |||
0 commit comments