Skip to contentSkip to conversation

wic: ensure generated disk system identifier is non-zero

PATCH In review Jonathan Liu 2 participants 7 messages Jul 28, 2017 – Jul 31, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 wic: ensure generated disk system identifier is non-zero

Zero may be interpreted as no MBR signature present and another partitioning program might install a new MBR signature.

---
 scripts/lib/wic/plugins/imager/direct.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/lib/wic/plugins/imager/direct.py +1 -1
index f20d8433f1..fe9b688ab0 100644
@@ -297,7 +297,7 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        self.identifier = int.from_bytes(os.urandom(4), 'little')
+        self.identifier = int.from_bytes(os.urandom(4), 'little') or 0xffffffff
 
         self.partitions = partitions
         self.partimages = []
Signature · 1 line
2.13.2
#1 lore raw
Quoted 13 lines · Jonathan Liu

Zero may be interpreted as no MBR signature present and another partitioning program might install a new MBR signature.

---
 scripts/lib/wic/plugins/imager/direct.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/lib/wic/plugins/imager/direct.py +0 -0
index f20d8433f1..fe9b688ab0 100644

scripts/lib/wic/plugins/imager/direct.py class PartitionedImage(): · line 301

Quoted 6 lines
scripts/lib/wic/plugins/imager/direct.py +1 -1
@@ -297,7 +297,7 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        self.identifier = int.from_bytes(os.urandom(4), 'little')
+        self.identifier = int.from_bytes(os.urandom(4), 'little') or 0xffffffff
 

Can we generate random identifier by calling os.urandom again if self.identifier is zero? Something like this, may be?

while True:
self.identifier = int.from_bytes(os.urandom(4), 'little') if self.identifier:
break

Assigning constant 0xffffffff can potentially create nonunique identifiers.

Quoted 10 lines
scripts/lib/wic/plugins/imager/direct.py +0 -0
         self.partitions = partitions
         self.partimages = []
Signature · 7 lines
2.13.2

-- 
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core
Signature · 3 lines
--
Regards,
Ed
#2 lore raw

Hi Ed,

Quoted 24 lines · Ed Bartosh
Quoted 13 lines · Jonathan Liu

Zero may be interpreted as no MBR signature present and another partitioning program might install a new MBR signature.

---
 scripts/lib/wic/plugins/imager/direct.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/lib/wic/plugins/imager/direct.py +0 -0
index f20d8433f1..fe9b688ab0 100644

scripts/lib/wic/plugins/imager/direct.py class PartitionedImage(): · line 301

Quoted 6 lines
scripts/lib/wic/plugins/imager/direct.py +1 -1
@@ -297,7 +297,7 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        self.identifier = int.from_bytes(os.urandom(4), 'little')
+        self.identifier = int.from_bytes(os.urandom(4), 'little') or 0xffffffff
 

Can we generate random identifier by calling os.urandom again if self.identifier is zero? Something like this, may be?

Quoted 7 lines

while True:
self.identifier = int.from_bytes(os.urandom(4), 'little') if self.identifier:
break

Assigning constant 0xffffffff can potentially create nonunique identifiers.

There is no guarantee that urandom will create unique identifiers. Infinite loop needs a timeout and error in the case it is unable to generate a suitable identifier. It is only 0xffffffff if it is 0. That means 0xffffffff is twice as likely to occur (2 in 4294967296 instead of 1 in 4294967296) so there is tiny bias. Note that this is how it is implemented for the DISK_SIGNATURE variable in OE and in GNU Parted as well.

Quoted 11 lines
Quoted 10 lines
scripts/lib/wic/plugins/imager/direct.py +0 -1
         self.partitions = partitions
         self.partimages = []
--

2.13.2

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Quoted 4 lines

--
--
Regards,
Ed

Regards,
Jonathan

#3 lore raw
Quoted 32 lines · Jonathan Liu

Hi Ed,

Quoted 29 lines · Ed Bartosh
Quoted 13 lines · Jonathan Liu

Zero may be interpreted as no MBR signature present and another partitioning program might install a new MBR signature.

---
 scripts/lib/wic/plugins/imager/direct.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/lib/wic/plugins/imager/direct.py +0 -0
index f20d8433f1..fe9b688ab0 100644

scripts/lib/wic/plugins/imager/direct.py class PartitionedImage(): · line 301

Quoted 6 lines
scripts/lib/wic/plugins/imager/direct.py +1 -1
@@ -297,7 +297,7 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        self.identifier = int.from_bytes(os.urandom(4), 'little')
+        self.identifier = int.from_bytes(os.urandom(4), 'little') or 0xffffffff
 

Can we generate random identifier by calling os.urandom again if self.identifier is zero? Something like this, may be?

while True:
self.identifier = int.from_bytes(os.urandom(4), 'little') if self.identifier:
break

Quoted 4 lines
Quoted 2 lines

Assigning constant 0xffffffff can potentially create nonunique identifiers.

There is no guarantee that urandom will create unique identifiers.

We can't have a guarantee with urandom, true. My point was that if you need random value it's better to call urandom again than using a constant value.

Quoted 2 lines

Infinite loop needs a timeout and error in the case it is unable to generate a suitable identifier.

I'm not sure it's needed, but it's not a big deal to add this. I really doubt os.urandom can generate long enough sequence of zeros.

Quoted 5 lines

It is only 0xffffffff if it is 0. That
means 0xffffffff is twice as likely to occur (2 in 4294967296 instead of 1 in 4294967296) so there is tiny bias. Note that this is how it is implemented for the DISK_SIGNATURE variable in OE and in GNU Parted as well.

Does this mean we shouldn't do better?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
scripts/lib/wic/plugins/imager/direct.py +0 -1
         self.partitions = partitions
         self.partimages = []
--

2.13.2

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Regards,
Jonathan

--
Regards,
Ed

#4 lore raw

Hi Ed,

Quoted 45 lines · Ed Bartosh
Quoted 32 lines · Jonathan Liu

Hi Ed,

Quoted 29 lines · Ed Bartosh
Quoted 13 lines · Jonathan Liu

Zero may be interpreted as no MBR signature present and another partitioning program might install a new MBR signature.

---
 scripts/lib/wic/plugins/imager/direct.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/lib/wic/plugins/imager/direct.py +0 -0
index f20d8433f1..fe9b688ab0 100644

scripts/lib/wic/plugins/imager/direct.py class PartitionedImage(): · line 301

Quoted 6 lines
scripts/lib/wic/plugins/imager/direct.py +1 -1
@@ -297,7 +297,7 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        self.identifier = int.from_bytes(os.urandom(4), 'little')
+        self.identifier = int.from_bytes(os.urandom(4), 'little') or 0xffffffff
 

Can we generate random identifier by calling os.urandom again if self.identifier is zero? Something like this, may be?

while True:
self.identifier = int.from_bytes(os.urandom(4), 'little') if self.identifier:
break

Quoted 4 lines
Quoted 2 lines

Assigning constant 0xffffffff can potentially create nonunique identifiers.

There is no guarantee that urandom will create unique identifiers.

We can't have a guarantee with urandom, true. My point was that if you need random value it's better to call urandom again than using a constant value.

Quoted 2 lines

Infinite loop needs a timeout and error in the case it is unable to generate a suitable identifier.

I'm not sure it's needed, but it's not a big deal to add this. I really doubt os.urandom can generate long enough sequence of zeros.

Quoted 7 lines
Quoted 5 lines

It is only 0xffffffff if it is 0. That
means 0xffffffff is twice as likely to occur (2 in 4294967296 instead of 1 in 4294967296) so there is tiny bias. Note that this is how it is implemented for the DISK_SIGNATURE variable in OE and in GNU Parted as well.

Does this mean we shouldn't do better?

How about random.SystemRandom().randrange(1, 0xffffffff) ?

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
scripts/lib/wic/plugins/imager/direct.py +0 -1
         self.partitions = partitions
         self.partimages = []
--

2.13.2

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Quoted 6 lines
Quoted 2 lines

Regards,
Jonathan

--
Regards,
Ed

Regards,
Jonathan

#5 lore raw
Quoted 53 lines · Jonathan Liu

Hi Ed,

Quoted 50 lines · Ed Bartosh
Quoted 32 lines · Jonathan Liu

Hi Ed,

Quoted 29 lines · Ed Bartosh
Quoted 13 lines · Jonathan Liu

Zero may be interpreted as no MBR signature present and another partitioning program might install a new MBR signature.

---
 scripts/lib/wic/plugins/imager/direct.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/lib/wic/plugins/imager/direct.py +0 -0
index f20d8433f1..fe9b688ab0 100644

scripts/lib/wic/plugins/imager/direct.py class PartitionedImage(): · line 301

Quoted 6 lines
scripts/lib/wic/plugins/imager/direct.py +1 -1
@@ -297,7 +297,7 @@ class PartitionedImage():
                           # all partitions (in bytes)
         self.ptable_format = ptable_format  # Partition table format
         # Disk system identifier
-        self.identifier = int.from_bytes(os.urandom(4), 'little')
+        self.identifier = int.from_bytes(os.urandom(4), 'little') or 0xffffffff
 

Can we generate random identifier by calling os.urandom again if self.identifier is zero? Something like this, may be?

while True:
self.identifier = int.from_bytes(os.urandom(4), 'little') if self.identifier:
break

Quoted 4 lines
Quoted 2 lines

Assigning constant 0xffffffff can potentially create nonunique identifiers.

There is no guarantee that urandom will create unique identifiers.

We can't have a guarantee with urandom, true. My point was that if you need random value it's better to call urandom again than using a constant value.

Quoted 2 lines

Infinite loop needs a timeout and error in the case it is unable to generate a suitable identifier.

I'm not sure it's needed, but it's not a big deal to add this. I really doubt os.urandom can generate long enough sequence of zeros.

Quoted 5 lines

It is only 0xffffffff if it is 0. That
means 0xffffffff is twice as likely to occur (2 in 4294967296 instead of 1 in 4294967296) so there is tiny bias. Note that this is how it is implemented for the DISK_SIGNATURE variable in OE and in GNU Parted as well.

Quoted 5 lines
Quoted 2 lines

Does this mean we shouldn't do better?

How about random.SystemRandom().randrange(1, 0xffffffff) ?

random.SystemRandom().randint(1, 0xffffffff) actually

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
scripts/lib/wic/plugins/imager/direct.py +0 -1
         self.partitions = partitions
         self.partimages = []
--

2.13.2

--
_______________________________________________
Openembedded-core mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-core

Quoted 6 lines
Quoted 2 lines

Regards,
Jonathan

--
Regards,
Ed

Regards,
Jonathan

#6 lore raw
Quoted 6 lines · Jonathan Liu
Quoted 3 lines

How about random.SystemRandom().randrange(1, 0xffffffff) ?

random.SystemRandom().randint(1, 0xffffffff) actually

This looks ok to me. Thanks.

--
Regards,
Ed