All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libblkid: make blkid_do_wipe() work with probes with offset
@ 2016-04-18 14:22 Petr Uzel
  2016-04-18 18:00 ` Karel Zak
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Uzel @ 2016-04-18 14:22 UTC (permalink / raw)
  To: util-linux; +Cc: Stanislav Brabec

When a probe is created with an offset, e.g. via
blkid_probe_set_device(), this offset is correctly used when looking for
the signatures, but is not respected by blkid_do_wipe() function.
Therefore the signature is removed from an invalid location.

Usecase: Wiping signatures from an area on the block device where
partition is to be created (but as it does not exist yet, there's no
device node for it and probe on the whole block device has to be used
with correct offset and length).

Reproducer:
======================== wiper.c ===========================

const char *dev;
unsigned long offset;
unsigned long size;

int main(int argc, char** argv) {

        if (argc != 4) {
                printf("usage: wiper dev offset size\n");
                exit(1);
        }

        dev = argv[1];
        offset = strtoull(argv[2], NULL, 10);
        size = strtoull(argv[3], NULL, 10);

        printf("dev=%s, off=%llu, size=%llu\n", dev, offset, size);

        int fd = open (dev, O_RDWR);
        if (fd == -1) {
                perror("open");
                exit(1);
        }

        blkid_loff_t wipe_offset = offset * SECTOR_SIZE;
        blkid_loff_t wipe_size = size * SECTOR_SIZE;

        int ret;

        blkid_probe pr;
        pr = blkid_new_probe();
        if (!pr)
                return 0;
        ret = blkid_probe_set_device(pr, fd, wipe_offset, wipe_size);
        ret = blkid_probe_enable_superblocks(pr, 1);
        ret = blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);

        while (blkid_do_probe(pr) == 0) {
                ret = blkid_do_wipe(pr, 0);
        }

        blkid_free_probe(pr);
        close(fd);
}
======================== wiper.c ===========================

Steps to reproduce:
modprobe scsi_debug
parted -s /dev/sdX mklabel gpt
parted -s /dev/sdX mkpart first 2048s 4095s
mkfs.ext2 /dev/sdX1

wipefs -np /dev/sdX1

./wiper /dev/sdX1 2048 2048

Actual result: wiper gets into endless loop, because
blkid_do_wipe() wipes at wrong location (1080), leaving the signature
on /dev/sdc1. So it is again found by blkid_do_probe(), and so on.

Expected result: wiper clears the ext2 signature at offset 1049656(=1080+2048*512).

Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
---
 libblkid/src/probe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c
index 34d97b8..0c8625c 100644
--- a/libblkid/src/probe.c
+++ b/libblkid/src/probe.c
@@ -1121,7 +1121,7 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
 	if (rc || len == 0 || off == NULL)
 		return 0;
 
-	offset = strtoumax(off, NULL, 10);
+	offset = strtoumax(off, NULL, 10) + pr->off;
 	fd = blkid_probe_get_fd(pr);
 	if (fd < 0)
 		return -1;
-- 
1.8.4.5


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] libblkid: make blkid_do_wipe() work with probes with offset
  2016-04-18 14:22 [PATCH] libblkid: make blkid_do_wipe() work with probes with offset Petr Uzel
@ 2016-04-18 18:00 ` Karel Zak
  2016-04-19  9:05   ` Petr Uzel
  0 siblings, 1 reply; 6+ messages in thread
From: Karel Zak @ 2016-04-18 18:00 UTC (permalink / raw)
  To: Petr Uzel; +Cc: util-linux, Stanislav Brabec

On Mon, Apr 18, 2016 at 04:22:05PM +0200, Petr Uzel wrote:
> When a probe is created with an offset, e.g. via
> blkid_probe_set_device(), this offset is correctly used when looking for
> the signatures, but is not respected by blkid_do_wipe() function.
> Therefore the signature is removed from an invalid location.

Wow, excellent catch. 

How did you found it? Do you use libblkid in some partitioning tool or
installer?

Applied, thanks!

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] libblkid: make blkid_do_wipe() work with probes with offset
  2016-04-18 18:00 ` Karel Zak
@ 2016-04-19  9:05   ` Petr Uzel
  2016-04-19  9:18     ` Karel Zak
  0 siblings, 1 reply; 6+ messages in thread
From: Petr Uzel @ 2016-04-19  9:05 UTC (permalink / raw)
  To: util-linux

[-- Attachment #1: Type: text/plain, Size: 983 bytes --]

On Mon, Apr 18, 2016 at 08:00:18PM +0200, Karel Zak wrote:
> On Mon, Apr 18, 2016 at 04:22:05PM +0200, Petr Uzel wrote:
> > When a probe is created with an offset, e.g. via
> > blkid_probe_set_device(), this offset is correctly used when looking for
> > the signatures, but is not respected by blkid_do_wipe() function.
> > Therefore the signature is removed from an invalid location.
> 
> Wow, excellent catch. 

Thanks, feels good to again contribute a fix after such a long time :)

> How did you found it? Do you use libblkid in some partitioning tool or
> installer?

Yes, I'm trying to implement a --wipesignatures for parted - this
would wipe the signatures from a new partition, _before_ the kernel
is informed. This is to avoid mess like RAID autoassembly...

[If only there was some sane mechanism lock a device to temporarily
prevent udev from touching it :/]

> 
> Applied, thanks!

Thanks,

        Petr


-- 
Petr Uzel
TL SUSE L3 Team 2

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] libblkid: make blkid_do_wipe() work with probes with offset
  2016-04-19  9:05   ` Petr Uzel
@ 2016-04-19  9:18     ` Karel Zak
  2016-04-22 14:38       ` Petr Uzel
  2016-05-13 13:50       ` Karel Zak
  0 siblings, 2 replies; 6+ messages in thread
From: Karel Zak @ 2016-04-19  9:18 UTC (permalink / raw)
  To: util-linux

On Tue, Apr 19, 2016 at 11:05:02AM +0200, Petr Uzel wrote:
> On Mon, Apr 18, 2016 at 08:00:18PM +0200, Karel Zak wrote:
> > On Mon, Apr 18, 2016 at 04:22:05PM +0200, Petr Uzel wrote:
> > > When a probe is created with an offset, e.g. via
> > > blkid_probe_set_device(), this offset is correctly used when looking for
> > > the signatures, but is not respected by blkid_do_wipe() function.
> > > Therefore the signature is removed from an invalid location.
> > 
> > Wow, excellent catch. 
> 
> Thanks, feels good to again contribute a fix after such a long time :)
> 
> > How did you found it? Do you use libblkid in some partitioning tool or
> > installer?
> 
> Yes, I'm trying to implement a --wipesignatures for parted - this
> would wipe the signatures from a new partition, _before_ the kernel
> is informed. This is to avoid mess like RAID autoassembly...
> 
> [If only there was some sane mechanism lock a device to temporarily
> prevent udev from touching it :/]

since v2.28 fdisk wipes disks (--wipe=auto,always,never), but it does
not care about partitions. 

Maybe we can improve it and add --wipe-partitions too. IMHO it should
not be enabled by default, because change disk layout and keep
filesystems seems like a valid use-case.

    Karel


-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] libblkid: make blkid_do_wipe() work with probes with offset
  2016-04-19  9:18     ` Karel Zak
@ 2016-04-22 14:38       ` Petr Uzel
  2016-05-13 13:50       ` Karel Zak
  1 sibling, 0 replies; 6+ messages in thread
From: Petr Uzel @ 2016-04-22 14:38 UTC (permalink / raw)
  To: util-linux

[-- Attachment #1: Type: text/plain, Size: 1497 bytes --]

On Tue, Apr 19, 2016 at 11:18:51AM +0200, Karel Zak wrote:
> On Tue, Apr 19, 2016 at 11:05:02AM +0200, Petr Uzel wrote:
> > On Mon, Apr 18, 2016 at 08:00:18PM +0200, Karel Zak wrote:
> > > On Mon, Apr 18, 2016 at 04:22:05PM +0200, Petr Uzel wrote:
> > > > When a probe is created with an offset, e.g. via
> > > > blkid_probe_set_device(), this offset is correctly used when looking for
> > > > the signatures, but is not respected by blkid_do_wipe() function.
> > > > Therefore the signature is removed from an invalid location.
> > > 
> > > Wow, excellent catch. 
> > 
> > Thanks, feels good to again contribute a fix after such a long time :)
> > 
> > > How did you found it? Do you use libblkid in some partitioning tool or
> > > installer?
> > 
> > Yes, I'm trying to implement a --wipesignatures for parted - this
> > would wipe the signatures from a new partition, _before_ the kernel
> > is informed. This is to avoid mess like RAID autoassembly...
> > 
> > [If only there was some sane mechanism lock a device to temporarily
> > prevent udev from touching it :/]
> 
> since v2.28 fdisk wipes disks (--wipe=auto,always,never), but it does
> not care about partitions. 
>
> Maybe we can improve it and add --wipe-partitions too. IMHO it should
> not be enabled by default, because change disk layout and keep
> filesystems seems like a valid use-case.

Agreed, it certainly has to be disabled by default.


        Petr

-- 
Petr Uzel
TL SUSE L3 Team 2

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] libblkid: make blkid_do_wipe() work with probes with offset
  2016-04-19  9:18     ` Karel Zak
  2016-04-22 14:38       ` Petr Uzel
@ 2016-05-13 13:50       ` Karel Zak
  1 sibling, 0 replies; 6+ messages in thread
From: Karel Zak @ 2016-05-13 13:50 UTC (permalink / raw)
  To: util-linux

On Tue, Apr 19, 2016 at 11:18:51AM +0200, Karel Zak wrote:
> Maybe we can improve it and add --wipe-partitions too. IMHO it should
> not be enabled by default, because change disk layout and keep
> filesystems seems like a valid use-case.

Implemented in git tree.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-05-13 13:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-18 14:22 [PATCH] libblkid: make blkid_do_wipe() work with probes with offset Petr Uzel
2016-04-18 18:00 ` Karel Zak
2016-04-19  9:05   ` Petr Uzel
2016-04-19  9:18     ` Karel Zak
2016-04-22 14:38       ` Petr Uzel
2016-05-13 13:50       ` Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.