cloud-init (18.2) fails on decoding proc1 env

Bug #1775371 reported by Kurt Garloff on 2018-06-06
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cloud-init
Medium
Unassigned

Bug Description

cloud-init-18.2 on an openSUSE-15 (python 3.6) kiwi image fails with this:

failed run of stage init-local
------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/cloudinit/cmd/main.py", line 655, in status_wrapper
    ret = functor(name, args)
  File "/usr/lib/python3.6/site-packages/cloudinit/cmd/main.py", line 222, in main_init
    network=not args.local)]
  File "/usr/lib/python3.6/site-packages/cloudinit/cmd/main.py", line 156, in attempt_cmdline_url
    cmdline = util.get_cmdline()
  File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 1351, in get_cmdline
    if is_container():
  File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 2075, in is_container
    pid1env = get_proc_env(1)
  File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 2109, in get_proc_env
    contents = load_file(fn)
  File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 1338, in load_file
    return decode_binary(contents)
  File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 150, in decode_binary
    return blob.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3432: invalid start byte
------------------------------------------------------------

This is from reading /proc/1/environ

hexdump around the relvant offset (3432 = 0x0d68) shows me:
00000d50 32 61 32 32 37 37 30 34 66 00 42 4f 4f 54 41 42 |2a227704f.BOOTAB|
00000d60 4c 45 5f 46 4c 41 47 3d 80 00 69 6e 69 74 3d 2f |LE_FLAG=..init=/|

So there we go: BOOTABLE_FLAG=\x80 does not decode to utf-8 ...

Related branches

Kurt Garloff (kgarloff) wrote :

Here's a patch that adds exception handling.
I guess there is no reason that would guarantee that the environment vars from process 1 (systemd/init) is UTF-8. so we need to handle whatever we see.
The good news is that detecting that we are in a container or not does not depend on correctly decoded strings ... we really only need to make sure that we don't error out.

Scott Moser (smoser) wrote :

Kurt,

Thanks for the good bug report and diagnosis.
decode_binary doensn't seem like the right place for this.

Heres a start of a patch to do that.

We'd like to add some tests for this to tests/unittests/test_util.py
I think the easiest way would be to just mock load_file .

If you want to push this along, please put up a merge proposal with a
unit test. If not, we'll get to it but not immediately.

Thanks again.
Scott

Changed in cloud-init:
status: New → Confirmed
importance: Undecided → Medium

Hi Scott,

Thanks for your quick response!
Thinking about it, I like your patch better than my own and definitely support it.
I'll try to find some time to come up with some unit tests - however please don't wait for it - I'm super-busy...

Thanks,
--
Kurt Garloff <email address hidden>, Cologne, Germany
(Sent from Mobile with K9.)

Am 6. Juni 2018 16:42:13 MESZ schrieb Scott Moser <email address hidden>:
>Kurt,
>
>Thanks for the good bug report and diagnosis.
>decode_binary doensn't seem like the right place for this.
>
>Heres a start of a patch to do that.
>
>We'd like to add some tests for this to tests/unittests/test_util.py
>I think the easiest way would be to just mock load_file .
>
>If you want to push this along, please put up a merge proposal with a
>unit test. If not, we'll get to it but not immediately.
>
>Thanks again.
>Scott
>
>
>** Patch added: "suggested fix"
>https://bugs.launchpad.net/cloud-init/+bug/1775371/+attachment/5149386/+files/suggested-1774371.patch
>
>** Changed in: cloud-init
> Status: New => Confirmed
>
>** Changed in: cloud-init
> Importance: Undecided => Medium
>
>--
>You received this bug notification because you are subscribed to the
>bug
>report.
>https://bugs.launchpad.net/bugs/1775371
>
>Title:
> cloud-init (18.2) fails on decoding proc1 env
>
>Status in cloud-init:
> Confirmed
>
>Bug description:
> cloud-init-18.2 on an openSUSE-15 (python 3.6) kiwi image fails with
> this:
>
> failed run of stage init-local
> ------------------------------------------------------------
> Traceback (most recent call last):
>File "/usr/lib/python3.6/site-packages/cloudinit/cmd/main.py", line
>655, in status_wrapper
> ret = functor(name, args)
>File "/usr/lib/python3.6/site-packages/cloudinit/cmd/main.py", line
>222, in main_init
> network=not args.local)]
>File "/usr/lib/python3.6/site-packages/cloudinit/cmd/main.py", line
>156, in attempt_cmdline_url
> cmdline = util.get_cmdline()
>File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 1351,
>in get_cmdline
> if is_container():
>File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 2075,
>in is_container
> pid1env = get_proc_env(1)
>File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 2109,
>in get_proc_env
> contents = load_file(fn)
>File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 1338,
>in load_file
> return decode_binary(contents)
>File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 150, in
>decode_binary
> return blob.decode(encoding)
>UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position
>3432: invalid start byte
> ------------------------------------------------------------
>
> This is from reading /proc/1/environ
>
> hexdump around the relvant offset (3432 = 0x0d68) shows me:
>00000d50 32 61 32 32 37 37 30 34 66 00 42 4f 4f 54 41 42
>|2a227704f.BOOTAB|
>00000d60 4c 45 5f 46 4c 41 47 3d 80 00 69 6e 69 74 3d 2f
>|LE_FLAG=..init=/|
>
> So there we go: BOOTABLE_FLAG=\x80 does not decode to utf-8 ...
>
>To manage notifications about this bug go to:
>https://bugs.launchpad.net/cloud-init/+bug/1775371/+subscriptions

Scott Moser (smoser) wrote :

An upstream commit landed for this bug.

To view that commit see the following URL:
https://git.launchpad.net/cloud-init/commit/?id=17137861

Changed in cloud-init:
status: Confirmed → Fix Committed

This bug is believed to be fixed in cloud-init in version 18.3. If this is still a problem for you, please make a comment and set the state back to New

Thank you.

Changed in cloud-init:
status: Fix Committed → Fix Released
boostrack (boostrack) wrote :

Hi there Scott,

Not sure if totally related to this issue but I am faced similar symptoms on tip of tree in github

Commit: [
5407e3901f3206071a101defce2d8d24e85cc7c0 netinfo: Adjust ifconfig output parsing for FreeBSD ipv
]

https://github.com/cloud-init/cloud-init/commit/fff37e7dc6849fd16db504b0d338fae20a7beb39

Without the patch provided by
https://bugs.launchpad.net/cloud-init/+bug/1775371/+attachment/5149292/+files/cloudinit-decodeerr-env1.diff

I face the same exception when using **query**

load_file
    return decode_binary(contents)
  File "/usr/lib/python3.6/site-packages/cloudinit/util.py", line 150, in decode_binary
    return blob.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3432: invalid start byte

e.g

cloud-init query --format 'custom-{{instance_id}}.{{region}}.{{availability_zone}}.{{v1.cloud_name}}.com{{local_hostname}}'

After patch it works as expected and query returns the formatted string

boostrack (boostrack) wrote :

Forgot to add version:

/usr/local/bin/cloud-init --version

/usr/local/bin/cloud-init 18.5

Thanks

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers