From nobody@FreeBSD.org  Wed May 14 22:04:19 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 605E4A5C
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 14 May 2014 22:04:19 +0000 (UTC)
Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client did not present a certificate)
	by mx1.freebsd.org (Postfix) with ESMTPS id 327572F90
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 14 May 2014 22:04:19 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s4EM4Ip5088405
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 14 May 2014 22:04:18 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s4EM4Ihq088404;
	Wed, 14 May 2014 22:04:18 GMT
	(envelope-from nobody)
Message-Id: <201405142204.s4EM4Ihq088404@cgiserv.freebsd.org>
Date: Wed, 14 May 2014 22:04:18 GMT
From: Garrett Cooper <yaneurabeya@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: nice(3) on FreeBSD returns EACCES instead of EPERM when provided negative prio's; is not compatible with Linux/NetBSD/POSIX
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         189821
>Category:       kern
>Synopsis:       [libc] [patch] nice(3) on FreeBSD returns EACCES instead of EPERM when provided negative prio's; is not compatible with Linux/NetBSD/POSIX
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed May 14 22:10:00 UTC 2014
>Closed-Date:    
>Last-Modified:  Wed May 14 22:20:01 UTC 2014
>Originator:     Garrett Cooper
>Release:        11.0-CURRENT
>Organization:
n/a
>Environment:
>Description:
The t_nice testcase from NetBSD fails when testing out negative priorities set with nice(3) because it's more or less a pass-through to setpriority(2). If one provides a negative priority to nice(3)/setpriority, it will return EACCES, not EPERM.

Linux/NetBSD/POSIX requires EPERM (before fix):

# kyua test t_nice
t_nice:nice_err  ->  failed: /usr/src/lib/libc/tests/gen/t_nice.c:85: Expected errno 1, got 13, in nice(i) == -1  [0.006s]
t_nice:nice_priority  ->  passed  [0.022s]
t_nice:nice_root  ->  passed  [0.005s]
t_nice:nice_thread  ->  passed  [0.007s]

3/4 passed (1 failed)
Committed action 42

After fix:

# env LD_PRELOAD=/usr/obj/usr/src/lib/libc/libc.so.7 kyua test t_nice
t_nice:nice_err  ->  passed  [0.005s]
t_nice:nice_priority  ->  passed  [0.039s]
t_nice:nice_root  ->  passed  [0.006s]
t_nice:nice_thread  ->  passed  [0.007s]

4/4 passed (0 failed)
Committed action 43

More discussion can be found here: https://github.com/yaneurabeya/freebsd/commit/0e07e7cd2d690af70a725cb869b4679d5d05cbc0#diff-414e52a1b185ec7b1c19140f302d99f8L82
>How-To-Repeat:
% sudo /bin/sh sh
% pw useradd notprivileged
% cat > test_unprivileged_negative_priority_with_nice.c <<EOF
#include <sys/resource.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int
main(void)
{

        if (geteuid() == 0) {
                printf("is root\n");
                return (1);
        }

        assert(nice(-1) == -1);
        assert(errno == EPERM);

        return (0);
}
EOF
% cc -o test_unprivileged_negative_priority_with_nice test_unprivileged_negative_priority_with_nice.c
% sudo -u notprivileged ./test_unprivileged_negative_priority_with_nice
% pw userdel notprivileged
>Fix:
See: https://github.com/yaneurabeya/freebsd/pull/6

>Release-Note:
>Audit-Trail:
>Unformatted:
