All of lore.kernel.org
 help / color / mirror / Atom feed
From: ramsdell@mitre.org (John D. Ramsdell)
To: Stephen Smalley <sds@tycho.nsa.gov>
Cc: selinux@tycho.nsa.gov
Subject: getfilecon return code
Date: 09 Jul 2007 14:07:30 -0400	[thread overview]
Message-ID: <ogtmyy5lacd.fsf@oolong.mitre.org> (raw)
In-Reply-To: <1183999553.12430.94.camel@moss-spartans.epoch.ncsc.mil>

I hadn't carefully read the manual page for getfilecon until now, but
I notice it states that a positive number is returned indicating the
number of bytes malloc'd for the context, and -1 is returned
indicating failure and that errno is set.  I would have guessed from
the description that zero is never an allowed return value.  In fact,
I wrote code that freecon'd a context whenever the return value was
not -1.

In the example below, when checking out a file in /proc/sys, zero is
returned and the result is a NULL context, something that need not be
free'd.  To me, failure to allocate a context feels like a failure
condition.

Perhaps the best strategy is to assume success when the context produced
in non-null, and not worry about the return code.

  security_context_t con = NULL;
  int rc = getfilecon(MP, &con);
  if (!con) /* Handle failure here */        
  else /* do something and then freecon(con) */

Is the following expression always true?

  (con != NULL) == (rc > 0)

John

[ramsdell@goo selinux]$ make
cc     mygetfilecon.c  /home/ramsdell/src/libselinux-2.0.8/src/libselinux.a -o mygetfilecon
[ramsdell@goo selinux]$ ./mygetfilecon
getfilecon("/proc/sys/kernel/pid_max", &con) = 0
con = NULL
[ramsdell@goo selinux]$ cat mygetfilecon.c
#include <stdio.h>
#include <selinux/selinux.h>

#define MP "/proc/sys/kernel/pid_max"

int
main(int argc, char **argv)
{
  security_context_t con;
  int rc = getfilecon(MP, &con);
  printf("getfilecon(\"%s\", &con) = %d\n", MP, rc);
  if (rc < 0)
    perror("getfilecon");
  else if (con)
    printf("con = \"%s\"\n", con);
  else
    printf("con = NULL\n", con);
  return 0;
}
[ramsdell@goo selinux]$ 

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

  parent reply	other threads:[~2007-07-09 18:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-09 16:45 [patch] libselinux: fix getfilecon handling of zero-length context Stephen Smalley
2007-07-09 16:48 ` Joshua Brindle
2007-07-09 16:50   ` Stephen Smalley
2007-07-09 17:39     ` Joshua Brindle
2007-07-09 18:07 ` John D. Ramsdell [this message]
2007-07-09 18:30   ` getfilecon return code Stephen Smalley
2007-07-09 18:42     ` Stephen Smalley
2007-07-09 19:13       ` Joshua Brindle
2007-07-10 12:41         ` Stephen Smalley
2007-07-10 17:49           ` Joshua Brindle
2007-09-12 15:43       ` Stephen Smalley
2007-07-09 20:01     ` John D. Ramsdell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ogtmyy5lacd.fsf@oolong.mitre.org \
    --to=ramsdell@mitre.org \
    --cc=sds@tycho.nsa.gov \
    --cc=selinux@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.