From 1418cade558ac4fd72282c9c462e178b44e62ef7 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 11 Jul 2019 23:26:02 -0400 Subject: [PATCH] file_systems: Pass size argument to ioctl in a lot more places. --- .../kernel/file_systems/bfs/Volume.cpp | 4 +- .../kernel/file_systems/btrfs/Volume.cpp | 4 +- src/add-ons/kernel/file_systems/cdda/cdda.cpp | 2 +- .../kernel/file_systems/exfat/Volume.cpp | 8 ++-- .../kernel/file_systems/ext2/Volume.cpp | 40 +++++++++---------- src/add-ons/kernel/file_systems/fat/dosfs.c | 2 +- .../kernel/file_systems/iso9660/iso9660.cpp | 5 ++- .../file_systems/udf/kernel_interface.cpp | 4 +- 8 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/add-ons/kernel/file_systems/bfs/Volume.cpp b/src/add-ons/kernel/file_systems/bfs/Volume.cpp index 5327b9b22d..ca3241fd24 100644 --- a/src/add-ons/kernel/file_systems/bfs/Volume.cpp +++ b/src/add-ons/kernel/file_systems/bfs/Volume.cpp @@ -102,7 +102,7 @@ DeviceOpener::Open(const char* device, int mode) if (_IsReadWrite(mode)) { // check out if the device really allows for read/write access device_geometry geometry; - if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry)) { + if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry))) { if (geometry.read_only) { // reopen device read-only close(fDevice); @@ -162,7 +162,7 @@ status_t DeviceOpener::GetSize(off_t* _size, uint32* _blockSize) { device_geometry geometry; - if (ioctl(fDevice, B_GET_GEOMETRY, &geometry) < 0) { + if (ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry)) < 0) { // maybe it's just a file struct stat stat; if (fstat(fDevice, &stat) < 0) diff --git a/src/add-ons/kernel/file_systems/btrfs/Volume.cpp b/src/add-ons/kernel/file_systems/btrfs/Volume.cpp index 4aff9ec0a7..c1bfbaf331 100644 --- a/src/add-ons/kernel/file_systems/btrfs/Volume.cpp +++ b/src/add-ons/kernel/file_systems/btrfs/Volume.cpp @@ -106,7 +106,7 @@ DeviceOpener::Open(const char* device, int mode) if (_IsReadWrite(mode)) { // check out if the device really allows for read/write access device_geometry geometry; - if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry)) { + if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry))) { if (geometry.read_only) { // reopen device read-only close(fDevice); @@ -166,7 +166,7 @@ status_t DeviceOpener::GetSize(off_t* _size, uint32* _blockSize) { device_geometry geometry; - if (ioctl(fDevice, B_GET_GEOMETRY, &geometry) < 0) { + if (ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry)) < 0) { // maybe it's just a file struct stat stat; if (fstat(fDevice, &stat) < 0) diff --git a/src/add-ons/kernel/file_systems/cdda/cdda.cpp b/src/add-ons/kernel/file_systems/cdda/cdda.cpp index 31bddb5ac7..0755d25c48 100644 --- a/src/add-ons/kernel/file_systems/cdda/cdda.cpp +++ b/src/add-ons/kernel/file_systems/cdda/cdda.cpp @@ -506,7 +506,7 @@ read_frames(int fd, off_t firstFrame, uint8 *buffer, size_t count) read.buffer = (char *)buffer; read.play = false; - if (ioctl(fd, B_SCSI_READ_CD, &read) < 0) { + if (ioctl(fd, B_SCSI_READ_CD, &read, sizeof(scsi_read_cd)) < 0) { // drive couldn't read data - try again to read with a smaller block size if (count == 1) return errno; diff --git a/src/add-ons/kernel/file_systems/exfat/Volume.cpp b/src/add-ons/kernel/file_systems/exfat/Volume.cpp index b6f7b1a7f3..65dcb39c6b 100644 --- a/src/add-ons/kernel/file_systems/exfat/Volume.cpp +++ b/src/add-ons/kernel/file_systems/exfat/Volume.cpp @@ -122,7 +122,7 @@ DeviceOpener::Open(const char* device, int mode) if (_IsReadWrite(mode)) { // check out if the device really allows for read/write access device_geometry geometry; - if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry)) { + if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry))) { if (geometry.read_only) { // reopen device read-only close(fDevice); @@ -182,7 +182,7 @@ status_t DeviceOpener::GetSize(off_t* _size, uint32* _blockSize) { device_geometry geometry; - if (ioctl(fDevice, B_GET_GEOMETRY, &geometry) < 0) { + if (ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry)) < 0) { // maybe it's just a file struct stat stat; if (fstat(fDevice, &stat) < 0) @@ -255,7 +255,7 @@ exfat_super_block::IsValid() return false; if (version_minor != 0 || version_major != 1) return false; - + return true; } @@ -480,7 +480,7 @@ Volume::GetIno(cluster_t cluster, uint32 offset, ino_t parent) node->parent = parent; fNodeTree.Insert(node); fInoTree.Insert(node); - TRACE("Volume::GetIno() new cluster %" B_PRIu32 " offset %" B_PRIu32 + TRACE("Volume::GetIno() new cluster %" B_PRIu32 " offset %" B_PRIu32 " ino %" B_PRIdINO "\n", cluster, offset, node->ino); return node->ino; } diff --git a/src/add-ons/kernel/file_systems/ext2/Volume.cpp b/src/add-ons/kernel/file_systems/ext2/Volume.cpp index 42eb9e3858..e14a24fecf 100644 --- a/src/add-ons/kernel/file_systems/ext2/Volume.cpp +++ b/src/add-ons/kernel/file_systems/ext2/Volume.cpp @@ -111,7 +111,7 @@ DeviceOpener::Open(const char* device, int mode) if (_IsReadWrite(mode)) { // check out if the device really allows for read/write access device_geometry geometry; - if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry)) { + if (!ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry))) { if (geometry.read_only) { // reopen device read-only close(fDevice); @@ -171,7 +171,7 @@ status_t DeviceOpener::GetSize(off_t* _size, uint32* _blockSize) { device_geometry geometry; - if (ioctl(fDevice, B_GET_GEOMETRY, &geometry) < 0) { + if (ioctl(fDevice, B_GET_GEOMETRY, &geometry, sizeof(device_geometry)) < 0) { // maybe it's just a file struct stat stat; if (fstat(fDevice, &stat) < 0) @@ -210,7 +210,7 @@ ext2_super_block::IsValid() || ReservedGDTBlocks() > (1UL << BlockShift()) / 4) { return false; } - + return true; } @@ -283,7 +283,7 @@ Volume::Mount(const char* deviceName, uint32 flags) { // flags |= B_MOUNT_READ_ONLY; // we only support read-only for now - + if ((flags & B_MOUNT_READ_ONLY) != 0) { TRACE("Volume::Mount(): Read only\n"); } else { @@ -312,7 +312,7 @@ Volume::Mount(const char* deviceName, uint32 flags) FATAL("Volume::Mount(): Identify() failed\n"); return status; } - + // check read-only features if mounting read-write if (!IsReadOnly() && _UnsupportedReadOnlyFeatures(fSuperBlock) != 0) return B_UNSUPPORTED; @@ -345,7 +345,7 @@ Volume::Mount(const char* deviceName, uint32 flags) TRACE("block size %" B_PRIu32 ", num groups %" B_PRIu32 ", groups per " "block %" B_PRIu32 ", first %" B_PRIu32 "\n", fBlockSize, fNumGroups, fGroupsPerBlock, fFirstDataBlock); - + uint32 blockCount = (fNumGroups + fGroupsPerBlock - 1) / fGroupsPerBlock; fGroupBlocks = (uint8**)malloc(blockCount * sizeof(uint8*)); @@ -366,7 +366,7 @@ Volume::Mount(const char* deviceName, uint32 flags) fBlockCache = opener.InitCache(NumBlocks(), fBlockSize); if (fBlockCache == NULL) return B_ERROR; - + TRACE("Volume::Mount(): Initialized block cache: %p\n", fBlockCache); // initialize journal if mounted read-write @@ -374,7 +374,7 @@ Volume::Mount(const char* deviceName, uint32 flags) (fSuperBlock.CompatibleFeatures() & EXT2_FEATURE_HAS_JOURNAL) != 0) { // TODO: There should be a mount option to ignore the existent journal if (fSuperBlock.JournalInode() != 0) { - fJournalInode = new(std::nothrow) Inode(this, + fJournalInode = new(std::nothrow) Inode(this, fSuperBlock.JournalInode()); if (fJournalInode == NULL) @@ -526,7 +526,7 @@ Volume::_UnsupportedIncompatibleFeatures(ext2_super_block& superBlock) | EXT2_INCOMPATIBLE_FEATURE_EXTENTS | EXT2_INCOMPATIBLE_FEATURE_FLEX_GROUP; /*| EXT2_INCOMPATIBLE_FEATURE_META_GROUP*/; - uint32 unsupported = superBlock.IncompatibleFeatures() + uint32 unsupported = superBlock.IncompatibleFeatures() & ~supportedIncompatible; if (unsupported != 0) { @@ -585,7 +585,7 @@ Volume::_GroupCheckSum(ext2_block_group *group, int32 index) checksum = calculate_crc(checksum, (uint8*)&number, sizeof(number)); checksum = calculate_crc(checksum, (uint8*)group, 30); if (Has64bitFeature()) { - checksum = calculate_crc(checksum, (uint8*)group + 34, + checksum = calculate_crc(checksum, (uint8*)group + 34, fGroupDescriptorSize - 34); } } @@ -621,13 +621,13 @@ Volume::GetBlockGroup(int32 index, ext2_block_group** _group) memcpy(fGroupBlocks[blockIndex], block, fBlockSize); TRACE("group [%" B_PRId32 "]: inode table %" B_PRIu64 "\n", index, - ((ext2_block_group*)(fGroupBlocks[blockIndex] + blockOffset + ((ext2_block_group*)(fGroupBlocks[blockIndex] + blockOffset * fGroupDescriptorSize))->InodeTable(Has64bitFeature())); } *_group = (ext2_block_group*)(fGroupBlocks[blockIndex] + blockOffset * fGroupDescriptorSize); - if (HasChecksumFeature() + if (HasChecksumFeature() && (*_group)->checksum != _GroupCheckSum(*_group, index)) { return B_BAD_DATA; } @@ -653,7 +653,7 @@ Volume::WriteBlockGroup(Transaction& transaction, int32 index) ext2_block_group *group = (ext2_block_group*)(fGroupBlocks[blockIndex] + blockOffset * fGroupDescriptorSize); - + group->checksum = _GroupCheckSum(group, index); TRACE("Volume::WriteBlockGroup() checksum 0x%x for group %" B_PRId32 " " "(free inodes %" B_PRIu32 ", unused %" B_PRIu32 ")\n", group->checksum, @@ -677,13 +677,13 @@ Volume::WriteBlockGroup(Transaction& transaction, int32 index) status_t Volume::ActivateLargeFiles(Transaction& transaction) { - if ((fSuperBlock.ReadOnlyFeatures() + if ((fSuperBlock.ReadOnlyFeatures() & EXT2_READ_ONLY_FEATURE_LARGE_FILE) != 0) return B_OK; - + fSuperBlock.SetReadOnlyFeatures(fSuperBlock.ReadOnlyFeatures() | EXT2_READ_ONLY_FEATURE_LARGE_FILE); - + return WriteSuperBlock(transaction); } @@ -691,13 +691,13 @@ Volume::ActivateLargeFiles(Transaction& transaction) status_t Volume::ActivateDirNLink(Transaction& transaction) { - if ((fSuperBlock.ReadOnlyFeatures() + if ((fSuperBlock.ReadOnlyFeatures() & EXT2_READ_ONLY_FEATURE_DIR_NLINK) != 0) return B_OK; - + fSuperBlock.SetReadOnlyFeatures(fSuperBlock.ReadOnlyFeatures() | EXT2_READ_ONLY_FEATURE_DIR_NLINK); - + return WriteSuperBlock(transaction); } @@ -734,7 +734,7 @@ Volume::RemoveOrphan(Transaction& transaction, ino_t id) ext2_inode* inode = (ext2_inode*)(block + InodeBlockIndex(currentID) * InodeSize()); - + if (currentID == id) { TRACE("Volume::RemoveOrphan(): First entry. Updating head to: %d\n", (int)inode->NextOrphan()); diff --git a/src/add-ons/kernel/file_systems/fat/dosfs.c b/src/add-ons/kernel/file_systems/fat/dosfs.c index bfa61481ab..f4062e4504 100644 --- a/src/add-ons/kernel/file_systems/fat/dosfs.c +++ b/src/add-ons/kernel/file_systems/fat/dosfs.c @@ -525,7 +525,7 @@ mount_fat_disk(const char *path, fs_volume *_vol, const int flags, } // get device characteristics - if (ioctl(fd, B_GET_GEOMETRY, &geo) < 0) { + if (ioctl(fd, B_GET_GEOMETRY, &geo, sizeof(device_geometry)) < 0) { struct stat st; if (fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) { /* support mounting disk images */ diff --git a/src/add-ons/kernel/file_systems/iso9660/iso9660.cpp b/src/add-ons/kernel/file_systems/iso9660/iso9660.cpp index f02a22c5c5..f88fa71b88 100644 --- a/src/add-ons/kernel/file_systems/iso9660/iso9660.cpp +++ b/src/add-ons/kernel/file_systems/iso9660/iso9660.cpp @@ -49,7 +49,7 @@ get_device_block_size(int fd) { device_geometry geometry; - if (ioctl(fd, B_GET_GEOMETRY, &geometry) < 0) { + if (ioctl(fd, B_GET_GEOMETRY, &geometry, sizeof(device_geometry)) < 0) { struct stat st; if (fstat(fd, &st) < 0 || S_ISDIR(st.st_mode)) return 0; @@ -628,7 +628,8 @@ ISOMount(const char *path, uint32 flags, iso9660_volume **_newVolume, /* try to open the raw device to get access to the other sessions as well */ if (volume->fdOfSession >= 0) { - if (ioctl(volume->fdOfSession, B_GET_PARTITION_INFO, &partitionInfo) < 0) { + if (ioctl(volume->fdOfSession, B_GET_PARTITION_INFO, &partitionInfo, + sizeof(partition_info)) < 0) { TRACE(("B_GET_PARTITION_INFO: ioctl returned error\n")); strcpy(partitionInfo.device, path); } diff --git a/src/add-ons/kernel/file_systems/udf/kernel_interface.cpp b/src/add-ons/kernel/file_systems/udf/kernel_interface.cpp index 7c0b6d40de..2ec9852a94 100644 --- a/src/add-ons/kernel/file_systems/udf/kernel_interface.cpp +++ b/src/add-ons/kernel/file_systems/udf/kernel_interface.cpp @@ -560,7 +560,7 @@ udf_mount(fs_volume *_volume, const char *_device, uint32 flags, // // If that fails, you're just SOL. - if (ioctl(device, B_GET_PARTITION_INFO, &info) == 0) { + if (ioctl(device, B_GET_PARTITION_INFO, &info, sizeof(partition_info)) == 0) { TRACE(("partition_info:\n")); TRACE(("\toffset: %Ld\n", info.offset)); TRACE(("\tsize: %Ld\n", info.size)); @@ -571,7 +571,7 @@ udf_mount(fs_volume *_volume, const char *_device, uint32 flags, _device = info.device; deviceOffset = info.offset / info.logical_block_size; numBlock = deviceOffset + info.size / info.logical_block_size; - } else if (ioctl(device, B_GET_GEOMETRY, &geometry) == 0) { + } else if (ioctl(device, B_GET_GEOMETRY, &geometry, sizeof(device_geometry)) == 0) { TRACE(("geometry_info:\n")); TRACE(("\tsectors_per_track: %ld\n", geometry.sectors_per_track)); TRACE(("\tcylinder_count: %ld\n", geometry.cylinder_count));