From patchwork Fri Mar 23 11:34:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yang xu X-Patchwork-Id: 889916 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cn.fujitsu.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4071gH6dyVz9s0q for ; Fri, 23 Mar 2018 22:35:22 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id C206F3E6AA8 for ; Fri, 23 Mar 2018 12:35:18 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [217.194.8.4]) by picard.linux.it (Postfix) with ESMTP id 24A7E3E662C for ; Fri, 23 Mar 2018 12:35:17 +0100 (CET) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-4.smtp.seeweb.it (Postfix) with ESMTP id 591731000C6D for ; Fri, 23 Mar 2018 12:35:13 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="38103681" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 23 Mar 2018 19:35:03 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 0119848AE76A for ; Fri, 23 Mar 2018 19:34:59 +0800 (CST) Received: from localhost.localdomain (10.167.220.182) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 23 Mar 2018 19:34:58 +0800 From: yang xu To: Date: Fri, 23 Mar 2018 19:34:48 +0800 Message-ID: <1521804892-5783-1-git-send-email-xuyang.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.7.1 MIME-Version: 1.0 X-Originating-IP: [10.167.220.182] X-yoursite-MailScanner-ID: 0119848AE76A.ABC72 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: xuyang.jy@cn.fujitsu.com X-Spam-Status: No, score=-0.0 required=7.0 tests=T_RP_MATCHES_RCVD autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 1/5] SAFE_MACROS: Add SAFE_SETREUID()/SAFE_SETREGID() X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: yang xu --- include/old/safe_macros.h | 6 ++++++ include/safe_macros_fn.h | 8 ++++++++ include/tst_safe_macros.h | 6 ++++++ lib/safe_macros.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/include/old/safe_macros.h b/include/old/safe_macros.h index e778d30..ad611f7 100644 --- a/include/old/safe_macros.h +++ b/include/old/safe_macros.h @@ -82,6 +82,12 @@ #define SAFE_SETUID(cleanup_fn, uid) \ safe_setuid(__FILE__, __LINE__, cleanup_fn, (uid)) +#define SAFE_SETREGID(rgid, egid) \ + safe_setregid(__FILE__, __LINE__, NULL, (rgid), (egid)) + +#define SAFE_SETREUID(ruid, euid) \ + safe_setreuid(__FILE__, __LINE__, NULL, (ruid), (euid)) + #define SAFE_GETRESUID(cleanup_fn, ruid, euid, suid) \ safe_getresuid(__FILE__, __LINE__, cleanup_fn, (ruid), (euid), (suid)) diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h index 3df9528..001f45c 100644 --- a/include/safe_macros_fn.h +++ b/include/safe_macros_fn.h @@ -83,6 +83,14 @@ int safe_setgid(const char *file, const int lineno, int safe_setuid(const char *file, const int lineno, void (*cleanup_fn)(void), uid_t uid); +int safe_setregid(const char *file, const int lineno, + void (*cleanup_fn)(void), + gid_t rgid, gid_t egid); + +int safe_setreuid(const char *file, const int lineno, + void (*cleanup_fn)(void), + uid_t ruid, uid_t euid); + int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void), uid_t *ruid, uid_t *euid, uid_t *suid); diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index f115a7b..e378c40 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -109,6 +109,12 @@ static inline int safe_dup(const char *file, const int lineno, #define SAFE_SETUID(uid) \ safe_setuid(__FILE__, __LINE__, NULL, (uid)) +#define SAFE_SETREGID(rgid, egid) \ + safe_setregid(__FILE__, __LINE__, NULL, (rgid), (egid)) + +#define SAFE_SETREUID(ruid, euid) \ + safe_setreuid(__FILE__, __LINE__, NULL, (ruid), (euid)) + #define SAFE_GETRESUID(ruid, euid, suid) \ safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid)) diff --git a/lib/safe_macros.c b/lib/safe_macros.c index abdeca0..0cf3a5f 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -318,6 +318,38 @@ int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void), return rval; } +int safe_setregid(const char *file, const int lineno, void (*cleanup_fn)(void), + gid_t rgid, gid_t egid) +{ + int rval; + + rval = setregid(rgid, egid); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, cleanup_fn, + "%s:%d: setregid(%p, %p) failed", + file, lineno, rgid, egid); + } + + return rval; +} + + +int safe_setreuid(const char *file, const int lineno, void (*cleanup_fn)(void), + uid_t ruid, uid_t euid) +{ + int rval; + + rval = setreuid(ruid, euid); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, cleanup_fn, + "%s:%d: setreuid(%p, %p) failed", + file, lineno, ruid, euid); + } + + return rval; +} + + int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void), uid_t *ruid, uid_t *euid, uid_t *suid) {