Page MenuHomeFreeBSD

Fix up pointer issues with lib/libkvm
ClosedPublic

Authored by ngie on Apr 15 2016, 1:36 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Apr 29, 8:37 PM
Unknown Object (File)
Mon, Apr 29, 8:36 PM
Unknown Object (File)
Mon, Apr 29, 8:18 PM
Unknown Object (File)
Mon, Apr 29, 8:18 PM
Unknown Object (File)
Mon, Apr 29, 12:53 AM
Unknown Object (File)
Dec 20 2023, 2:14 AM
Unknown Object (File)
Nov 18 2023, 8:59 PM
Unknown Object (File)
Nov 15 2023, 7:27 PM

Details

Reviewers
pfg
jhb
Summary

Fix up pointer issues with lib/libkvm

In particular,

  • avoid dereferencing NULL pointers
  • test pointers against NULL, not 0
  • test for errout == NULL in the top-level functions (kvm_open, kvm_openfiles, kvm_open2, etc)

Found with: devel/cocchinelle

MFC after: 1 week
Sponsored by: EMC / Isilon Storage Division

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 3393
Build 3430: arc lint + arc unit

Event Timeline

ngie retitled this revision from to Fix up pointer issues with lib/libkvm.
ngie updated this object.
ngie edited the test plan for this revision. (Show Details)
ngie added a reviewer: pfg.
ngie added subscribers: bdrewery, markj.
ngie added inline comments.
lib/libkvm/kvm.c
487–490

I should add a note about this.. in particular, _kvm_open handles the errout == NULL case, but none of the top-level functions did in this case

lib/libkvm/kvm.c
382–385

These should be NULL.

lib/libkvm/kvm_proc.c
640

Oy. Missed this case

642

This should be NULL, not 0.

649

Should be reallocf.

651

This should be NULL... now it makes sense why this wasn't working with coccinelle...

ngie marked 6 inline comments as done.
  • Fix some accidentally overlooked items
  • Use reallocf instead of realloc[+free on failure]

Hi,

I have two dummy questions:

  1. Is it really necessary those #if #else?
  2. At line 640 would not hurt keep that if(kd->procbase) before call free. NOTE: I didn't make any test with the code, I don't really know if always when call _kvm_freeprocs() the pointer will have data.

Best,

lib/libkvm/kvm.c
487–490

I've never considered errout an optional argument here. The manpage for kvm_open() has more details, but kvm_open() is a legacy API for compatiblity with ancient SunOS and uses errstr == NULL to mean that errors should not be printed to stderr by the library directly.

The more recent kvm_openfiles() and kvm_open2() do not ever print errors from the library to stderr directly. Instead, callers use kvm_geterror() to access error messages. However, 'errstr' must point to a valid buffer of the desired size to handle errors during open since kvm_geterror() requires an open kvm handle.

OTOH, it seems that the inconsistency in _kvm_open() and kvm_openfiles() goes back to FreeBSD 1.1 at least.

Looking in CSRG history, it seems that this goes back to some commit by Kirk of a patch from someone else to add the kvm_open() compat layer to the BSD libkvm. Prior to that there was just a global errbuf variable that kvm_geterr() returned.

lib/libkvm/kvm_file.c
74 ↗(On Diff #15210)

I think kvm_file.c should just be removed in 11 instead (as a separate commit). I wouldn't bother touching it in this one.

lib/libkvm/kvm_proc.c
650–651

Since you've uninlined reallocf() here, you could in theory just reuse p and not bother with 'np' at all:

p = reallocf(p, n);

However, I don't think it really matters either way.

Hi,

I have two dummy questions:

  1. Is it really necessary those #if #else?
  2. At line 640 would not hurt keep that if(kd->procbase) before call free. NOTE: I didn't make any test with the code, I don't really know if always when call _kvm_freeprocs() the pointer will have data.

Best,

For 1), I vote to kill kvm_file.c and kvm_getfile() in 11.

For 2), free() is defined to be a no-op if you pass in NULL, so it's ok to remove the if.

ngie added inline comments.
lib/libkvm/kvm_file.c
74 ↗(On Diff #15210)

kvm_getfiles needs to be removed then as it's a public API. Should I do that?

lib/libkvm/kvm.c
487–490

I understand the concern, but unfortunately kvm(3) doesn't enforce that requirement (it's assumed).

Should errout be tested for != NULL and return EINVAL?

lib/libkvm/kvm.c
487–490

Your approach is probably the most flexible to allow the error reporting to be optional, so you can leave it as is.

lib/libkvm/kvm_file.c
74 ↗(On Diff #15210)

Yes. It hasn't worked in a long time. We've already bumped libkvm's major version to add kvm_open2() and friends for cross-debugging support, so it's actually not a bad time to do this.

Fix some more missing 0 -> NULL conversions in kvm_args

Remove unrelated kvm.h diff

jhb added a reviewer: jhb.
This revision is now accepted and ready to land.Apr 22 2016, 5:00 PM
ngie marked 3 inline comments as done.