#define PR_CAP_AMBIENT_CLEAR_ALL 4
#endif /* !defined(PR_CAP_AMBIENT) */
static bool initNsKeepCaps(cap_user_data_t cap_data) {
- char dbgmsg[4096];
/* Copy all permitted caps to the inheritable set */
- dbgmsg[0] = '\0';
+ std::string dbgmsg1;
for (const auto& i : capNames) {
if (getPermitted(cap_data, i.val)) {
- util::sSnPrintf(dbgmsg, sizeof(dbgmsg), " %s", i.name);
+ util::StrAppend(&dbgmsg1, " %s", i.name);
setInheritable(cap_data, i.val);
}
}
- LOG_D("Adding the following capabilities to the inheritable set:%s", dbgmsg);
+ LOG_D("Adding the following capabilities to the inheritable set:%s", dbgmsg1.c_str());
if (!setCaps(cap_data)) {
return false;
}
/* Make sure the inheritable set is preserved across execve via the ambient set */
- dbgmsg[0] = '\0';
+ std::string dbgmsg2;
for (const auto& i : capNames) {
if (!getPermitted(cap_data, i.val)) {
continue;
-1) {
PLOG_W("prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, %s)", i.name);
} else {
- util::sSnPrintf(dbgmsg, sizeof(dbgmsg), " %s", i.name);
+ util::StrAppend(&dbgmsg2, " %s", i.name);
}
}
- LOG_D("Added the following capabilities to the ambient set:%s", dbgmsg);
+ LOG_D("Added the following capabilities to the ambient set:%s", dbgmsg2.c_str());
return true;
}
if (((flags & ~(knownFlagMask)) == 0) && !res.empty()) {
res.pop_back();
} else {
- char flagstr[32];
- snprintf(flagstr, sizeof(flagstr), "%#tx", flags & ~(knownFlagMask));
- res.append(flagstr);
+ util::StrAppend(&res, "%#tx", flags & ~(knownFlagMask));
}
return res;
}
if (flags & ~(knownFlagMask)) {
- char flagstr[32];
- snprintf(flagstr, sizeof(flagstr), "%#tx|", flags & ~(knownFlagMask));
- res.append(flagstr);
+ util::StrAppend(&res, "%#tx|", flags & ~(knownFlagMask));
}
res.append(util::sigName(flags & CSIGNAL).c_str());
return res;
return snprintf(str, size, "%s%s", buf1, buf2);
}
+std::string* StrAppend(std::string* str, const char* format, ...) {
+ char* strp;
+
+ va_list args;
+ va_start(args, format);
+ int ret = vasprintf(&strp, format, args);
+ va_end(args);
+
+ if (ret == -1) {
+ PLOG_E("Memory allocation failed during asprintf()");
+ str->append(" [ERROR: mem_allocation_failed] ");
+ return str;
+ }
+
+ str->append(strp, ret);
+ free(strp);
+ return str;
+}
+
bool isANumber(const char* s) {
for (size_t i = 0; s[i]; s++) {
if (!isdigit(s[i]) && s[i] != 'x') {
ssize_t writeToFd(int fd, const void* buf, size_t len);
bool writeBufToFile(const char* filename, const void* buf, size_t len, int open_flags);
bool createDirRecursively(const char* dir);
-int sSnPrintf(char* str, size_t size, const char* format, ...);
+int sSnPrintf(char* str, size_t size, const char* format, ...)
+ __attribute__((format(printf, 3, 4)));
+std::string* StrAppend(std::string* str, const char* format, ...)
+ __attribute__((format(printf, 2, 3)));
bool isANumber(const char* s);
uint64_t rnd64(void);
const std::string sigName(int signo);