Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion include/advisor-annotate.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ typedef HMODULE lib_t;
#define __itt_load_lib(name) LoadLibraryA(name)
#define __itt_unload_lib(handle) FreeLibrary(handle)
#define __itt_system_error() (int)GetLastError()
#define __annotate_is_secure_execution_context() (0)
#endif /* ANNOTATE_DECLARE */

#else /* defined(WIN32) || defined(_WIN32) */
Expand All @@ -129,13 +130,44 @@ typedef HMODULE lib_t;
#include <pthread.h>
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>

typedef void* lib_t;

#define __itt_get_proc(lib, name) dlsym(lib, name)
#define __itt_load_lib(name) dlopen(name, RTLD_LAZY)
#define __itt_unload_lib(handle) dlclose(handle)
#define __itt_system_error() errno

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#define __ANNOTATE_ISSETUGID_AVAILABLE 1
#elif defined(__has_include)
#if __has_include(<sys/auxv.h>)
#include <sys/auxv.h>
#define __ANNOTATE_GETAUXVAL_AVAILABLE 1
#endif
#elif defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
#include <sys/auxv.h>
#define __ANNOTATE_GETAUXVAL_AVAILABLE 1
#endif

#if defined(__ANNOTATE_GETAUXVAL_AVAILABLE) && !defined(AT_SECURE)
#define AT_SECURE 23 /* as defined by <elf.h> */
#endif

/* A process which gained privileges on exec() keeps the environment of the less
* privileged user who started it, so it must not take the library from there. */
static __inline int __annotate_is_secure_execution_context(void)
{
#if defined(__ANNOTATE_ISSETUGID_AVAILABLE)
if (issetugid() != 0)
return 1;
#elif defined(__ANNOTATE_GETAUXVAL_AVAILABLE)
if (getauxval(AT_SECURE) != 0)
return 1;
#endif
return (getuid() != geteuid() || getgid() != getegid()) ? 1 : 0;
}
#endif /* ANNOTATE_DECLARE */

#endif /* defined(WIN32) || defined(_WIN32) */
Expand Down Expand Up @@ -301,7 +333,9 @@ __annotate_routines_init(struct __annotate_routines* itt) {
char* lib_name = NULL;
lib_t itt_notify = 0;

lib_name = getenv("INTEL_LIBITTNOTIFY64");
if (!__annotate_is_secure_execution_context()) {
lib_name = getenv("INTEL_LIBITTNOTIFY64");
}

if (lib_name) {
itt_notify = __itt_load_lib(lib_name);
Expand Down
32 changes: 32 additions & 0 deletions include/fortran/advisor_annotate.f90
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,28 @@ function get_library_entry(library, proc_name) bind(C, name="dlsym")
character(kind=C_CHAR), dimension(*), intent(in) :: proc_name
end function get_library_entry

! Process credentials, used to detect a privilege elevated process.

function get_real_uid() bind(C, name="getuid")
import
integer(kind=C_INT) :: get_real_uid
end function get_real_uid

function get_effective_uid() bind(C, name="geteuid")
import
integer(kind=C_INT) :: get_effective_uid
end function get_effective_uid

function get_real_gid() bind(C, name="getgid")
import
integer(kind=C_INT) :: get_real_gid
end function get_real_gid

function get_effective_gid() bind(C, name="getegid")
import
integer(kind=C_INT) :: get_effective_gid
end function get_effective_gid

!dec$ endif

end interface
Expand Down Expand Up @@ -637,7 +659,17 @@ subroutine load_itt_library
type(C_PTR) :: library
character*1024 ittnotify_path

ittnotify_path = ''
!dec$ if defined(WIN32) .or. defined(_WIN32)
call getenv('INTEL_LIBITTNOTIFY64',ittnotify_path)
!dec$ else
! A process which gained privileges on exec() keeps the environment of
! the less privileged user who started it.
if (get_real_uid() == get_effective_uid() .and. &
get_real_gid() == get_effective_gid()) then
call getenv('INTEL_LIBITTNOTIFY64',ittnotify_path)
endif
!dec$ endif
if ( ittnotify_path /= '' ) then
! print *,' libpath: "'//trim(ittnotify_path)//'"'
!dec$ if defined(WIN32) .or. defined(_WIN32)
Expand Down
4 changes: 3 additions & 1 deletion include/ittnotify.h
Original file line number Diff line number Diff line change
Expand Up @@ -4670,8 +4670,10 @@ typedef enum __itt_error_code
/* %1$s -- env var name, %2$d -- system error. */
__itt_error_env_too_long = 5, /*!< variable value too long */
/* %1$s -- env var name, %2$d -- actual length of the var, %3$d -- max allowed length. */
__itt_error_system = 6 /*!< pthread_mutexattr_init or pthread_mutex_init failed */
__itt_error_system = 6, /*!< pthread_mutexattr_init or pthread_mutex_init failed */
/* %1$s -- function name, %2$d -- errno. */
__itt_error_env_ignored = 7 /*!< env var ignored in a privilege elevated process */
/* %1$s -- env var name. */
} __itt_error_code;

typedef void (__itt_error_handler_t)(__itt_error_code code, va_list);
Expand Down
47 changes: 45 additions & 2 deletions src/ittnotify/ittnotify_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@
#define ITT_MAGIC { 0xED, 0xAB, 0xAB, 0xEC, 0x0D, 0xEE, 0xDA, 0x30 }

/* Replace with snapshot date YYYYMMDD for promotion build. */
#define API_VERSION_BUILD 20260603
#define API_VERSION_BUILD 20260903

#ifndef API_VERSION_NUM
#define API_VERSION_NUM 3.28.2
#define API_VERSION_NUM 3.28.3
#endif /* API_VERSION_NUM */

#define API_VERSION "ITT-API-Version " ITT_TO_STR(API_VERSION_NUM) \
Expand Down Expand Up @@ -380,6 +380,49 @@ pthread_t pthread_self(void) __attribute__((weak));

#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

/* A process which gained privileges on exec() - setuid/setgid, file
* capabilities, MAC transition - keeps the environment of the less privileged
* user who started it, so it must not take the library to load from there.
*/
#if ITT_PLATFORM==ITT_PLATFORM_WIN

#define __itt_is_secure_execution_context() (0)

#else /* ITT_PLATFORM!=ITT_PLATFORM_WIN */

#include <unistd.h>

#if ITT_PLATFORM==ITT_PLATFORM_MAC || ITT_PLATFORM==ITT_PLATFORM_FREEBSD || ITT_PLATFORM==ITT_PLATFORM_OPENBSD
#define ITT_ISSETUGID_AVAILABLE 1
#elif defined(__has_include)
#if __has_include(<sys/auxv.h>)
#include <sys/auxv.h>
#define ITT_GETAUXVAL_AVAILABLE 1
#endif
#elif defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
#include <sys/auxv.h>
#define ITT_GETAUXVAL_AVAILABLE 1
#endif

#if defined(ITT_GETAUXVAL_AVAILABLE) && !defined(AT_SECURE)
#define AT_SECURE 23 /* as defined by <elf.h> */
#endif

ITT_INLINE int __itt_is_secure_execution_context(void) ITT_INLINE_ATTRIBUTE;
ITT_INLINE int __itt_is_secure_execution_context(void)
{
#if defined(ITT_ISSETUGID_AVAILABLE)
if (issetugid() != 0)
return 1;
#elif defined(ITT_GETAUXVAL_AVAILABLE)
if (getauxval(AT_SECURE) != 0)
return 1;
#endif
return (getuid() != geteuid() || getgid() != getegid()) ? 1 : 0;
}

#endif /* ITT_PLATFORM==ITT_PLATFORM_WIN */

/* strdup() is not included into C99 which results in a compiler warning about
* implicitly declared symbol. To avoid the issue strdup is implemented
* manually.
Expand Down
5 changes: 5 additions & 0 deletions src/ittnotify/ittnotify_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,11 @@ static const char* __itt_get_env_var(const char* name)

if (name != NULL)
{
if (__itt_is_secure_execution_context())
{
__itt_report_error(__itt_error_env_ignored, name);
return NULL;
}
#if ITT_PLATFORM==ITT_PLATFORM_WIN
size_t max_len = MAX_ENV_VALUE_SIZE - (size_t)(env_value - env_buff);
DWORD rc = GetEnvironmentVariableA(name, env_value, (DWORD)max_len);
Expand Down
7 changes: 6 additions & 1 deletion src/ittnotify/jitprofiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ static int isValidAbsolutePath(char *path, size_t maxPathLength)

if (pathLength > 2)
{
if (isalpha(path[0]) && path[1] == ':' && path[2] == '\\')
if (isalpha((unsigned char)path[0]) && path[1] == ':' && path[2] == '\\')
{
return 1;
}
Expand Down Expand Up @@ -173,6 +173,11 @@ static int loadiJIT_Funcs()
m_libHandle = NULL;
}

if (__itt_is_secure_execution_context())
{
return 0;
}

/* Try to get the dll name from the environment */
#if ITT_PLATFORM==ITT_PLATFORM_WIN
dNameLength = GetEnvironmentVariableA(NEW_DLL_ENVIRONMENT_VAR, NULL, 0);
Expand Down
5 changes: 3 additions & 2 deletions src/ittnotify_refcol/itt_refcol_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ static void ref_collector_init(void)
if (!g_ref_collector_logger.init_state)
{
static char file_name_buffer[LOG_BUFFER_MAX_SIZE*2];
char* gen_json = getenv(env_gen_json);
int env_trusted = !__itt_is_secure_execution_context();
char* gen_json = env_trusted ? getenv(env_gen_json) : NULL;
g_ref_collector_logger.gen_json = (gen_json != NULL && atoi(gen_json) != 0);

char* log_dir = getenv(env_log_dir);
char* log_dir = env_trusted ? getenv(env_log_dir) : NULL;
char* log_file = generate_output_file_name();
if (log_file == NULL)
{
Expand Down
Loading