Bug 15653

Summary: Field confusion in control files
Product: [Yocto Project Subprojects] opkg Reporter: Michal Sieron <michalwsieron>
Component: opkgAssignee: Alex Stewart <alex.stewart>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: Undecided    
Version: 1.0   
Target Milestone: ---   
Hardware: All   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: No (bug/feature does not impact docs)
Attachments:
Description Flags
Fix for field confusion in control files none

Description Michal Sieron 2024-11-19 20:49:42 UTC
Created attachment 5082 [details]
Fix for field confusion in control files

*Below is a cover letter I wrote before I learned opgk patches don't go
through the mailing list. Attached is patch with proposed fix for
described issue*

A little bit of background: we have a custom field in our ipk control
files named `Package_name` with a value of the ipk filename, e.g.:
    Package_name: libc6_2.35-r0.ipk

The problem addressed in this patch is happening, because opkg only
checks if first `n` bytes match `Package` string and doesn't care
whether that string is actually followed by a colon (`:`) or not.

This was especially bad in our case as because of our custom field opkg
was cutting the field at the underscore in `Package_name` and was using
what followed as the actual package name. So in that example of
`Package_name: libc6_2.35-r0.ipk` resulting name of the installed
package was `name: libc6_2.35-r0.ipk`, which of course is totally wrong.

What's more, because of the space in that parsed package name, all
(pre|post)(install|rm) hooks were broken, as they go through shell in
`pkg.c:pkg_run_script()`, which uses `-c` argument. Because there
is no escaping, it led to error like "no file named `name:`".
You can see this is caused by a space in the wrongly parsed package name,
which is used as a prefix in name for all `postinstall` hooks and similar.

In my opinion that `-c` should be either removed mandating writing those
hooks as a shell scripts or the whole shell call replaced with a direct
binary execution.

As I am not yet sure, which approach is right here, I am only tackling
name confusion issue here.
Comment 1 Alex Stewart 2024-11-21 20:02:55 UTC
Hey Michal; thanks for the patch. Patches *do* actually go through the mailing list at [1]. Though I could pull it from here, I would prefer that it go to the list, just to give others in the community the opportunity to speak about it. Could you resubmit there?

[1] https://lists.yoctoproject.org/g/opkg


Regarding this version of the patch:

Overall, the issue and fix make sense.

What happens if someone calls this `is_field()` implementation with `type` and `line` values that are the same, and do not end with a colon? eg.

```
is_field("foo", "foo);
```

It seems like the new `strncmp("foo", "foo", 3)` would succeed and pass through to the colon checks, where the second case (`line[3] == ':'`) would try to index into an undefined memory region. So you might have to do some more processing to avoid an index error.
Comment 2 Michal Sieron 2024-11-21 23:36:33 UTC
> Patches *do* actually go through the mailing list

I really don't know how I missed that...

> Could you resubmit there?

Sure, just did that.

> What happens if someone calls this `is_field()` implementation with `type`
> and `line` values that are the same, and do not end with a colon? eg.
> 
> ```
> is_field("foo", "foo);
> ```
> 
> It seems like the new `strncmp("foo", "foo", 3)` would succeed and pass
> through to the colon checks, where the second case (`line[3] == ':'`) would
> try to index into an undefined memory region. So you might have to do some
> more processing to avoid an index error.

Well, it wouldn't access undefined memory, as it would read the NULL terminator of the `line`, which would be different from `:` and in that case we would conclude the field doesn't match.
Comment 3 Alex Stewart 2024-11-22 20:51:00 UTC
Hmm... I was thinking of situations where a null-terminator might not be applied to the end of the string. But on further research, none of those should apply here. So, you're correct. Objection dropped.
Comment 4 Alex Stewart 2025-01-17 20:06:07 UTC
Fixed as of opkg v0.8.0, by commit c87188d7535684fddb8cf80993c147b215602b63.

https://git.yoctoproject.org/opkg/commit/?id=c87188d7535684fddb8cf80993c147b215602b63