Patchwork [2/2] qga: add windows implementation for guest-set-time

login
register
mail settings
Submitter Lei Li
Date March 12, 2013, 9:08 a.m.
Message ID <1363079306-27589-3-git-send-email-lilei@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/226846/
State New
Headers show

Comments

Lei Li - March 12, 2013, 9:08 a.m.
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 qga/commands-win32.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
Eric Blake - March 12, 2013, 3:05 p.m.
On 03/12/2013 03:08 AM, Lei Li wrote:
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  qga/commands-win32.c | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
> 

> +void qmp_guest_set_time(int64_t time_ns, Error **errp)
> +{
> +    SYSTEMTIME ts;
> +    FILETIME tf;
> +    LONGLONG time;
> +
> +    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
> +    if (error_is_set(errp)) {
> +        error_setg(errp, "Failed to acquire privilege");
> +        return;
> +    }

Earlier, you told me that acquire_privilege is auto-dropped after a
successful SetSystemTime.  But here, you acquire the privilege...

> +
> +    if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
> +        error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
> +        return;

...then return early without ever relinquishing it.

> +    }
> +
> +    time = time_ns / 100 + W32_FT_OFFSET;
> +
> +    tf.dwLowDateTime = (DWORD) time;
> +    tf.dwHighDateTime = (DWORD) (time >> 32);
> +
> +    if (!FileTimeToSystemTime(&tf, &ts)) {
> +        error_setg(errp, "Failed to convert system time");
> +        return;
> +    }

I would reorder the acquire_privilege to here, to give us the best
possible chance of avoiding a leak of privileges when the user passes
bogus data.

> +
> +    if (!SetSystemTime(&ts)) {
> +        slog("guest-set-time failed: %d", GetLastError());
> +        error_setg_errno(errp, errno, "Failed to set time to guest");
> +        return;
> +    }
> +}
> +
>  int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
>  {
>      error_set(err, QERR_UNSUPPORTED);
>
Lei Li - March 13, 2013, 8:56 a.m.
On 03/12/2013 11:05 PM, Eric Blake wrote:
> On 03/12/2013 03:08 AM, Lei Li wrote:
>> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
>> ---
>>   qga/commands-win32.c | 34 ++++++++++++++++++++++++++++++++++
>>   1 file changed, 34 insertions(+)
>>
>> +void qmp_guest_set_time(int64_t time_ns, Error **errp)
>> +{
>> +    SYSTEMTIME ts;
>> +    FILETIME tf;
>> +    LONGLONG time;
>> +
>> +    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
>> +    if (error_is_set(errp)) {
>> +        error_setg(errp, "Failed to acquire privilege");
>> +        return;
>> +    }
> Earlier, you told me that acquire_privilege is auto-dropped after a
> successful SetSystemTime.  But here, you acquire the privilege...
>
>> +
>> +    if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
>> +        error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
>> +        return;
> ...then return early without ever relinquishing it.
>
>> +    }
>> +
>> +    time = time_ns / 100 + W32_FT_OFFSET;
>> +
>> +    tf.dwLowDateTime = (DWORD) time;
>> +    tf.dwHighDateTime = (DWORD) (time >> 32);
>> +
>> +    if (!FileTimeToSystemTime(&tf, &ts)) {
>> +        error_setg(errp, "Failed to convert system time");
>> +        return;
>> +    }
> I would reorder the acquire_privilege to here, to give us the best
> possible chance of avoiding a leak of privileges when the user passes
> bogus data.

It make sense, I should thought about this, thank you!

>> +
>> +    if (!SetSystemTime(&ts)) {
>> +        slog("guest-set-time failed: %d", GetLastError());
>> +        error_setg_errno(errp, errno, "Failed to set time to guest");
>> +        return;
>> +    }
>> +}
>> +
>>   int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
>>   {
>>       error_set(err, QERR_UNSUPPORTED);
>>

Patch

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index 0a2bb34..e000324 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -140,6 +140,40 @@  int64_t qmp_guest_get_time(Error **errp)
    return time_ns;
 }
 
+void qmp_guest_set_time(int64_t time_ns, Error **errp)
+{
+    SYSTEMTIME ts;
+    FILETIME tf;
+    LONGLONG time;
+
+    acquire_privilege(SE_SYSTEMTIME_NAME, errp);
+    if (error_is_set(errp)) {
+        error_setg(errp, "Failed to acquire privilege");
+        return;
+    }
+
+    if (time_ns < 0 || time_ns / 100 > INT64_MAX - W32_FT_OFFSET) {
+        error_setg(errp, "Time %" PRId64 "is invalid", time_ns);
+        return;
+    }
+
+    time = time_ns / 100 + W32_FT_OFFSET;
+
+    tf.dwLowDateTime = (DWORD) time;
+    tf.dwHighDateTime = (DWORD) (time >> 32);
+
+    if (!FileTimeToSystemTime(&tf, &ts)) {
+        error_setg(errp, "Failed to convert system time");
+        return;
+    }
+
+    if (!SetSystemTime(&ts)) {
+        slog("guest-set-time failed: %d", GetLastError());
+        error_setg_errno(errp, errno, "Failed to set time to guest");
+        return;
+    }
+}
+
 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
 {
     error_set(err, QERR_UNSUPPORTED);