All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paul <paul_c@domain.hid>
To: xenomai@xenomai.org
Subject: Re: [Xenomai-help] Xenomai and mlockall
Date: Mon, 25 Jun 2007 19:04:04 +0100	[thread overview]
Message-ID: <200706251904.04510.paul_c@domain.hid> (raw)
In-Reply-To: <467FBC67.8030805@domain.hid>

[-- Attachment #1: Type: text/plain, Size: 379 bytes --]

On Monday 25 June 2007 14:00, Johan Borkhuis wrote:
> Is there a way to "tweak" the CAP_IPC_LOCK capability of the system or
> the task so that I can run mlockall call, or is there a way to disable
> this check on Xenomai?

See attached source - It needs to be linked to libcap. Once compiled, set 
user/group to root along with the sticky flag (chmod a+s).


Regards, Paul.





[-- Attachment #2: capabilities_demo.c --]
[-- Type: text/x-csrc, Size: 3064 bytes --]

/********************************************************************
*
* Description: capabilities_demo.c
*
*	Based on trivial-periodic.c from Xenomai's examples/native
*       directory - Additional material for dropping root privileges
*       and communicating with a kernel task subject to the following
*       statement:
*
* Author: Paul Corner <paul_c@domain.hid>
* Created on: Thu Mar 29 12:21:00 BST 2007
* License: GPL Ver. 2
*    
* Copyright (c) 2007 Paul Corner <paul_c@domain.hid>  All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
********************************************************************/

#include "autoconf.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>

#include <native/task.h>
#include <native/heap.h>
#include <native/timer.h>

#define TASK_PRIO 10

void catch_signal(int sig)
{
}


#if HAVE_LIBCAP
#include <sys/capability.h>
#include <sys/prctl.h>
#endif

void set_security(void)
{
#if HAVE_LIBCAP
    cap_t cap;

    /* Running as root - No need to drop anything. */
    if (getuid() == 0)
        return;

    /* Do a `chown root` and `chmod a+s` to allow non-root use */
    if (geteuid() != 0) {
	printf("suid not set - aborting");
	exit(-EPERM);
    }

    /* keep root capabilities in the transition to non-root user */
    prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
    setuid(getuid());

    /* drop all privs except CAP_SYS_NICE (for Xenomai), CAP_IPC_LOCK       
        (for mlockall), and CAP_SYS_RAWIO (for ioperm/iopl) for all
        current and future ops - Note: If all IO is done in kernel space,
        CAP_SYS_RAWIO can be dropped. */
    cap = cap_from_text("CAP_SYS_RAWIO,CAP_IPC_LOCK,CAP_SYS_NICE+ep");
    if (errno)
	perror("cap_from_text failed");
    if (cap_set_proc(cap) < 0) {
	perror("Failed to drop root privileges, aborting");
	exit(-EPERM);
    }

    cap_free(cap);
#endif
    return;
}

RT_HEAP driver_heap;

int main(int argc, char *argv[])
{
    int err = 0;
    int t, k, s;
    void* mem = NULL;
    struct driver_info *info;
    struct driver_data *data;
    RT_HEAP_INFO heap_info;

    signal(SIGTERM, catch_signal);
    signal(SIGINT, catch_signal);

    set_security();

    /* Avoids memory swapping for this program */
    mlockall(MCL_CURRENT | MCL_FUTURE);
    rt_task_create(&demo_task, "trivial", 0, TASK_PRIO, 0);
    rt_task_start(&demo_task, &demo, NULL);

    pause();

    return err;
}

  parent reply	other threads:[~2007-06-25 18:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-25 13:00 [Xenomai-help] Xenomai and mlockall Johan Borkhuis
     [not found] ` <cbe23c50706250904i57afbc0xfbce52fdb9dccc66@domain.hid>
2007-06-25 16:05   ` [Xenomai-help] Fwd: " Eric Noulard
2007-06-26  6:10     ` Johan Borkhuis
2007-06-25 18:04 ` Paul [this message]
2007-06-26  7:26 ` [Xenomai-help] " Philippe Gerum
  -- strict thread matches above, loose matches on Subject: below --
2007-06-26  8:53 Fillod Stephane

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=200706251904.04510.paul_c@domain.hid \
    --to=paul_c@domain.hid \
    --cc=xenomai@xenomai.org \
    /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.